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