Changes in uspace/drv/char/ns8250/ns8250.c [56fd7cf:c7235d40] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/ns8250/ns8250.c
r56fd7cf rc7235d40 58 58 #include <ops/char_dev.h> 59 59 60 #include <devman.h> 60 61 #include <ns.h> 62 #include <ipc/devman.h> 61 63 #include <ipc/services.h> 62 64 #include <ipc/irc.h> … … 80 82 /** Interrupt ID Register definition. */ 81 83 #define NS8250_IID_ACTIVE (1 << 0) 82 #define NS8250_IID_CAUSE_MASK 0x0e83 #define NS8250_IID_CAUSE_RXSTATUS 0x0684 84 85 85 /** FIFO Control Register definition. */ … … 125 125 #define NS8250_MSR_SIGNALS (NS8250_MSR_CTS | NS8250_MSR_DSR \ 126 126 | NS8250_MSR_RI | NS8250_MSR_DCD) 127 128 /** Obtain soft-state structure from function node */ 129 #define NS8250(fnode) ((ns8250_t *) ((fnode)->dev->driver_data)) 130 131 /** Obtain soft-state structure from device node */ 132 #define NS8250_FROM_DEV(dnode) ((ns8250_t *) ((dnode)->driver_data)) 127 133 128 134 /** The number of bits of one data unit send by the serial port. */ … … 173 179 /** The fibril mutex for synchronizing the access to the device. */ 174 180 fibril_mutex_t mutex; 175 /** Indicates that some data has become available */176 fibril_condvar_t input_buffer_available;177 181 /** True if device is removed. */ 178 182 bool removed; 179 183 } ns8250_t; 180 184 181 /** Obtain soft-state structure from device node */182 static ns8250_t *dev_ns8250(ddf_dev_t *dev)183 {184 return ddf_dev_data_get(dev);185 }186 187 /** Obtain soft-state structure from function node */188 static ns8250_t *fun_ns8250(ddf_fun_t *fun)189 {190 return dev_ns8250(ddf_fun_get_dev(fun));191 }192 193 185 /** Find out if there is some incomming data available on the serial port. 194 186 * … … 245 237 static int ns8250_read(ddf_fun_t *fun, char *buf, size_t count) 246 238 { 247 ns8250_t *ns = fun_ns8250(fun); 248 int ret = 0; 249 250 if (count == 0) return 0; 239 ns8250_t *ns = NS8250(fun); 240 int ret = EOK; 251 241 252 242 fibril_mutex_lock(&ns->mutex); 253 while (buf_is_empty(&ns->input_buffer))254 fibril_condvar_wait(&ns->input_buffer_available, &ns->mutex);255 243 while (!buf_is_empty(&ns->input_buffer) && (size_t)ret < count) { 256 244 buf[ret] = (char)buf_pop_front(&ns->input_buffer); … … 283 271 static int ns8250_write(ddf_fun_t *fun, char *buf, size_t count) 284 272 { 285 ns8250_t *ns = fun_ns8250(fun);273 ns8250_t *ns = NS8250(fun); 286 274 size_t idx; 287 275 … … 321 309 static void ns8250_dev_cleanup(ns8250_t *ns) 322 310 { 311 if (ns->dev->parent_sess) { 312 async_hangup(ns->dev->parent_sess); 313 ns->dev->parent_sess = NULL; 314 } 323 315 } 324 316 … … 330 322 static bool ns8250_pio_enable(ns8250_t *ns) 331 323 { 332 ddf_msg(LVL_DEBUG, "ns8250_pio_enable %s", ddf_dev_get_name(ns->dev));324 ddf_msg(LVL_DEBUG, "ns8250_pio_enable %s", ns->dev->name); 333 325 334 326 /* Gain control over port's registers. */ … … 336 328 (void **) &ns->port)) { 337 329 ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32 338 " for device %s.", ns->io_addr, ddf_dev_get_name(ns->dev));330 " for device %s.", ns->io_addr, ns->dev->name); 339 331 return false; 340 332 } … … 352 344 static bool ns8250_dev_probe(ns8250_t *ns) 353 345 { 354 ddf_msg(LVL_DEBUG, "ns8250_dev_probe %s", ddf_dev_get_name(ns->dev));346 ddf_msg(LVL_DEBUG, "ns8250_dev_probe %s", ns->dev->name); 355 347 356 348 bool res = true; … … 372 364 if (!res) { 373 365 ddf_msg(LVL_DEBUG, "Device %s is not present.", 374 ddf_dev_get_name(ns->dev));366 ns->dev->name); 375 367 } 376 368 … … 385 377 static int ns8250_dev_initialize(ns8250_t *ns) 386 378 { 387 async_sess_t *parent_sess; 379 ddf_msg(LVL_DEBUG, "ns8250_dev_initialize %s", ns->dev->name); 380 388 381 int ret = EOK; 389 390 ddf_msg(LVL_DEBUG, "ns8250_dev_initialize %s", ddf_dev_get_name(ns->dev));391 382 392 383 hw_resource_list_t hw_resources; … … 394 385 395 386 /* Connect to the parent's driver. */ 396 parent_sess = ddf_dev_parent_sess_create(ns->dev, EXCHANGE_SERIALIZE); 397 if (parent_sess == NULL) { 387 ns->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE, 388 ns->dev->handle, IPC_FLAG_BLOCKING); 389 if (!ns->dev->parent_sess) { 398 390 ddf_msg(LVL_ERROR, "Failed to connect to parent driver of " 399 "device %s.", ddf_dev_get_name(ns->dev));391 "device %s.", ns->dev->name); 400 392 ret = ENOENT; 401 393 goto failed; … … 403 395 404 396 /* Get hw resources. */ 405 ret = hw_res_get_resource_list( parent_sess, &hw_resources);397 ret = hw_res_get_resource_list(ns->dev->parent_sess, &hw_resources); 406 398 if (ret != EOK) { 407 399 ddf_msg(LVL_ERROR, "Failed to get HW resources for device " 408 "%s.", ddf_dev_get_name(ns->dev));400 "%s.", ns->dev->name); 409 401 goto failed; 410 402 } … … 422 414 irq = true; 423 415 ddf_msg(LVL_NOTE, "Device %s was asigned irq = 0x%x.", 424 ddf_dev_get_name(ns->dev), ns->irq);416 ns->dev->name, ns->irq); 425 417 break; 426 418 … … 429 421 if (res->res.io_range.size < REG_COUNT) { 430 422 ddf_msg(LVL_ERROR, "I/O range assigned to " 431 "device %s is too small.", ddf_dev_get_name(ns->dev));423 "device %s is too small.", ns->dev->name); 432 424 ret = ELIMIT; 433 425 goto failed; … … 435 427 ioport = true; 436 428 ddf_msg(LVL_NOTE, "Device %s was asigned I/O address = " 437 "0x%x.", ddf_dev_get_name(ns->dev), ns->io_addr);429 "0x%x.", ns->dev->name, ns->io_addr); 438 430 break; 439 431 … … 445 437 if (!irq || !ioport) { 446 438 ddf_msg(LVL_ERROR, "Missing HW resource(s) for device %s.", 447 ddf_dev_get_name(ns->dev));439 ns->dev->name); 448 440 ret = ENOENT; 449 441 goto failed; … … 468 460 { 469 461 /* Interrupt when data received. */ 470 pio_write_8(®s->ier, NS8250_IER_RXREADY | NS8250_IER_RXSTATUS);462 pio_write_8(®s->ier, NS8250_IER_RXREADY); 471 463 pio_write_8(®s->mcr, NS8250_MCR_DTR | NS8250_MCR_RTS 472 464 | NS8250_MCR_OUT2); … … 507 499 async_exchange_end(exch); 508 500 509 /* Read LSR to clear possible previous LSR interrupt */510 pio_read_8(&ns->regs->lsr);511 512 501 /* Enable interrupt on the serial port. */ 513 502 ns8250_port_interrupts_enable(ns->regs); … … 613 602 *parity = ((val >> NS8250_LCR_PARITY) & 7); 614 603 615 /* Silence warnings */616 *word_length = 0;617 618 604 switch (val & 3) { 619 605 case WORD_LENGTH_5: … … 709 695 /* 8 bits, no parity, two stop bits. */ 710 696 ns8250_port_set_com_props(ns->regs, SERIAL_NO_PARITY, 8, 2); 711 /* 712 * Enable FIFO, clear them, with 4-byte threshold for greater 713 * reliability. 714 */ 697 /* Enable FIFO, clear them, with 14-byte threshold. */ 715 698 pio_write_8(&ns->regs->iid, NS8250_FCR_FIFOENABLE 716 | NS8250_FCR_RXFIFORESET | NS8250_FCR_TXFIFORESET 717 | NS8250_FCR_RXTRIGGERLOW );699 | NS8250_FCR_RXFIFORESET | NS8250_FCR_TXFIFORESET 700 | NS8250_FCR_RXTRIGGERLOW | NS8250_FCR_RXTRIGGERHI); 718 701 /* 719 702 * RTS/DSR set (Request to Send and Data Terminal Ready lines enabled), … … 748 731 bool cont = true; 749 732 750 fibril_mutex_lock(&ns->mutex);751 733 while (cont) { 734 fibril_mutex_lock(&ns->mutex); 735 752 736 cont = ns8250_received(regs); 753 737 if (cont) { … … 755 739 756 740 if (ns->client_connected) { 757 bool buf_was_empty = buf_is_empty(&ns->input_buffer);758 741 if (!buf_push_back(&ns->input_buffer, val)) { 759 742 ddf_msg(LVL_WARN, "Buffer overflow on " 760 "%s.", ddf_dev_get_name(ns->dev)); 761 break; 743 "%s.", ns->dev->name); 762 744 } else { 763 745 ddf_msg(LVL_DEBUG2, "Character %c saved " 764 746 "to the buffer of %s.", 765 val, ddf_dev_get_name(ns->dev)); 766 if (buf_was_empty) 767 fibril_condvar_broadcast(&ns->input_buffer_available); 747 val, ns->dev->name); 768 748 } 769 749 } 770 750 } 771 } 772 fibril_mutex_unlock(&ns->mutex); 773 fibril_yield(); 751 752 fibril_mutex_unlock(&ns->mutex); 753 fibril_yield(); 754 } 774 755 } 775 756 776 757 /** The interrupt handler. 777 758 * 778 * The serial port is initialized to interrupt when some data come or line 779 * status register changes, so the interrupt is handled by reading the incoming 780 * data and reading the line status register. 759 * The serial port is initialized to interrupt when some data come, so the 760 * interrupt is handled by reading the incomming data. 781 761 * 782 762 * @param dev The serial port device. … … 785 765 ipc_call_t *icall) 786 766 { 787 ns8250_t *ns = dev_ns8250(dev); 788 789 uint8_t iir = pio_read_8(&ns->regs->iid); 790 if ((iir & NS8250_IID_CAUSE_MASK) == NS8250_IID_CAUSE_RXSTATUS) { 791 uint8_t lsr = pio_read_8(&ns->regs->lsr); 792 if (lsr & NS8250_LSR_OE) { 793 ddf_msg(LVL_WARN, "Overrun error on %s", ddf_dev_get_name(ns->dev)); 794 } 795 } 796 797 ns8250_read_from_device(ns); 767 ns8250_read_from_device(NS8250_FROM_DEV(dev)); 798 768 } 799 769 … … 831 801 832 802 ddf_msg(LVL_DEBUG, "ns8250_dev_add %s (handle = %d)", 833 d df_dev_get_name(dev), (int) ddf_dev_get_handle(dev));803 dev->name, (int) dev->handle); 834 804 835 805 /* Allocate soft-state for the device */ … … 841 811 842 812 fibril_mutex_initialize(&ns->mutex); 843 fibril_condvar_initialize(&ns->input_buffer_available);844 813 ns->dev = dev; 845 814 … … 886 855 887 856 /* Set device operations. */ 888 ddf_fun_set_ops(fun, &ns8250_dev_ops);857 fun->ops = &ns8250_dev_ops; 889 858 rc = ddf_fun_bind(fun); 890 859 if (rc != EOK) { … … 898 867 899 868 ddf_msg(LVL_NOTE, "Device %s successfully initialized.", 900 d df_dev_get_name(dev));869 dev->name); 901 870 902 871 return EOK; … … 911 880 static int ns8250_dev_remove(ddf_dev_t *dev) 912 881 { 913 ns8250_t *ns = dev_ns8250(dev);882 ns8250_t *ns = NS8250_FROM_DEV(dev); 914 883 int rc; 915 884 … … 945 914 static int ns8250_open(ddf_fun_t *fun) 946 915 { 947 ns8250_t *ns = fun_ns8250(fun);916 ns8250_t *ns = NS8250(fun); 948 917 int res; 949 918 … … 971 940 static void ns8250_close(ddf_fun_t *fun) 972 941 { 973 ns8250_t *data = fun_ns8250(fun);942 ns8250_t *data = (ns8250_t *) fun->dev->driver_data; 974 943 975 944 fibril_mutex_lock(&data->mutex); … … 996 965 unsigned int *word_length, unsigned int* stop_bits) 997 966 { 998 ns8250_t *data = dev_ns8250(dev);967 ns8250_t *data = (ns8250_t *) dev->driver_data; 999 968 ns8250_regs_t *regs = data->regs; 1000 969 … … 1027 996 stop_bits); 1028 997 1029 ns8250_t *data = dev_ns8250(dev);998 ns8250_t *data = (ns8250_t *) dev->driver_data; 1030 999 ns8250_regs_t *regs = data->regs; 1031 1000 int ret; … … 1056 1025 switch (method) { 1057 1026 case SERIAL_GET_COM_PROPS: 1058 ns8250_get_props( ddf_fun_get_dev(fun), &baud_rate, &parity, &word_length,1027 ns8250_get_props(fun->dev, &baud_rate, &parity, &word_length, 1059 1028 &stop_bits); 1060 1029 async_answer_4(callid, EOK, baud_rate, parity, word_length, … … 1067 1036 word_length = IPC_GET_ARG3(*call); 1068 1037 stop_bits = IPC_GET_ARG4(*call); 1069 ret = ns8250_set_props( ddf_fun_get_dev(fun), baud_rate, parity, word_length,1038 ret = ns8250_set_props(fun->dev, baud_rate, parity, word_length, 1070 1039 stop_bits); 1071 1040 async_answer_0(callid, ret); … … 1084 1053 static void ns8250_init(void) 1085 1054 { 1086 ddf_log_init(NAME, LVL_ WARN);1055 ddf_log_init(NAME, LVL_ERROR); 1087 1056 1088 1057 ns8250_dev_ops.open = &ns8250_open;
Note:
See TracChangeset
for help on using the changeset viewer.