Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/char/ns8250/ns8250.c

    r5b68e0c r1dc4a5e  
    111111        /** The fibril mutex for synchronizing the access to the device. */
    112112        fibril_mutex_t mutex;
    113         /** True if device is removed. */
    114         bool removed;
    115113} ns8250_t;
     114
     115/** Create per-device soft-state structure.
     116 *
     117 * @return      Pointer to soft-state structure.
     118 */
     119static ns8250_t *ns8250_new(void)
     120{
     121        ns8250_t *ns;
     122       
     123        ns = (ns8250_t *) calloc(1, sizeof(ns8250_t));
     124        if (ns == NULL)
     125                return NULL;
     126       
     127        fibril_mutex_initialize(&ns->mutex);
     128        return ns;
     129}
     130
     131/** Delete soft-state structure.
     132 *
     133 * @param ns    The driver data structure.
     134 */
     135static void ns8250_delete(ns8250_t *ns)
     136{
     137        assert(ns != NULL);
     138        free(ns);
     139}
    116140
    117141/** Find out if there is some incomming data available on the serial port.
     
    221245
    222246static int ns8250_add_device(ddf_dev_t *dev);
    223 static int ns8250_dev_remove(ddf_dev_t *dev);
    224247
    225248/** The serial port device driver's standard operations. */
    226249static driver_ops_t ns8250_ops = {
    227         .add_device = &ns8250_add_device,
    228         .dev_remove = &ns8250_dev_remove
     250        .add_device = &ns8250_add_device
    229251};
    230252
     
    616638}
    617639
    618 /** Deinitialize the serial port device.
    619  *
    620  * @param ns            Serial port device
    621  */
    622 static void ns8250_port_cleanup(ns8250_t *ns)
    623 {
    624         /* Disable FIFO */
    625         pio_write_8(ns->port + 2, 0x00);
    626         /* Disable DTR, RTS, OUT1, OUT2 (int. enable) */
    627         pio_write_8(ns->port + 4, 0x00);
    628         /* Disable all interrupts from the port */
    629         ns8250_port_interrupts_disable(ns->port);
    630 }
    631 
    632640/** Read the data from the serial port device and store them to the input
    633641 * buffer.
     
    713721       
    714722        /* Allocate soft-state for the device */
    715         ns = ddf_dev_data_alloc(dev, sizeof(ns8250_t));
     723        ns = ns8250_new();
    716724        if (ns == NULL) {
    717725                rc = ENOMEM;
     
    719727        }
    720728       
    721         fibril_mutex_initialize(&ns->mutex);
    722729        ns->dev = dev;
     730        dev->driver_data = ns;
    723731       
    724732        rc = ns8250_dev_initialize(ns);
     
    784792        if (need_cleanup)
    785793                ns8250_dev_cleanup(ns);
     794        if (ns != NULL)
     795                ns8250_delete(ns);
    786796        return rc;
    787 }
    788 
    789 static int ns8250_dev_remove(ddf_dev_t *dev)
    790 {
    791         ns8250_t *ns = NS8250_FROM_DEV(dev);
    792         int rc;
    793        
    794         fibril_mutex_lock(&ns->mutex);
    795         if (ns->client_connected) {
    796                 fibril_mutex_unlock(&ns->mutex);
    797                 return EBUSY;
    798         }
    799         ns->removed = true;
    800         fibril_mutex_unlock(&ns->mutex);
    801        
    802         rc = ddf_fun_unbind(ns->fun);
    803         if (rc != EOK) {
    804                 ddf_msg(LVL_ERROR, "Failed to unbind function.");
    805                 return rc;
    806         }
    807        
    808         ddf_fun_destroy(ns->fun);
    809        
    810         ns8250_port_cleanup(ns);
    811         ns8250_unregister_interrupt_handler(ns);
    812         ns8250_dev_cleanup(ns);
    813         return EOK;
    814797}
    815798
     
    823806static int ns8250_open(ddf_fun_t *fun)
    824807{
    825         ns8250_t *ns = NS8250(fun);
     808        ns8250_t *data = (ns8250_t *) fun->dev->driver_data;
    826809        int res;
    827810       
    828         fibril_mutex_lock(&ns->mutex);
    829         if (ns->client_connected) {
     811        fibril_mutex_lock(&data->mutex);
     812        if (data->client_connected) {
    830813                res = ELIMIT;
    831         } else if (ns->removed) {
    832                 res = ENXIO;
    833814        } else {
    834815                res = EOK;
    835                 ns->client_connected = true;
    836         }
    837         fibril_mutex_unlock(&ns->mutex);
     816                data->client_connected = true;
     817        }
     818        fibril_mutex_unlock(&data->mutex);
    838819       
    839820        return res;
Note: See TracChangeset for help on using the changeset viewer.