Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * driver for the GE IP-OCTAL boards
0004  *
0005  * Copyright (C) 2009-2012 CERN (www.cern.ch)
0006  * Author: Nicolas Serafini, EIC2 SA
0007  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/sched.h>
0014 #include <linux/tty.h>
0015 #include <linux/serial.h>
0016 #include <linux/tty_flip.h>
0017 #include <linux/slab.h>
0018 #include <linux/io.h>
0019 #include <linux/ipack.h>
0020 #include "ipoctal.h"
0021 #include "scc2698.h"
0022 
0023 #define IP_OCTAL_ID_SPACE_VECTOR    0x41
0024 #define IP_OCTAL_NB_BLOCKS          4
0025 
0026 static const struct tty_operations ipoctal_fops;
0027 
0028 struct ipoctal_channel {
0029     struct ipoctal_stats        stats;
0030     unsigned int            nb_bytes;
0031     wait_queue_head_t       queue;
0032     spinlock_t          lock;
0033     unsigned int            pointer_read;
0034     unsigned int            pointer_write;
0035     struct tty_port         tty_port;
0036     bool                tty_registered;
0037     union scc2698_channel __iomem   *regs;
0038     union scc2698_block __iomem *block_regs;
0039     unsigned int            board_id;
0040     u8              isr_rx_rdy_mask;
0041     u8              isr_tx_rdy_mask;
0042     unsigned int            rx_enable;
0043 };
0044 
0045 struct ipoctal {
0046     struct ipack_device     *dev;
0047     unsigned int            board_id;
0048     struct ipoctal_channel      channel[NR_CHANNELS];
0049     struct tty_driver       *tty_drv;
0050     u8 __iomem          *mem8_space;
0051     u8 __iomem          *int_space;
0052 };
0053 
0054 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
0055                           unsigned int index)
0056 {
0057     return container_of(chan, struct ipoctal, channel[index]);
0058 }
0059 
0060 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
0061 {
0062     iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
0063     channel->rx_enable = 0;
0064     iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
0065     iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
0066     iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
0067     iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
0068 }
0069 
0070 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
0071 {
0072     struct ipoctal_channel *channel;
0073 
0074     channel = dev_get_drvdata(tty->dev);
0075 
0076     /*
0077      * Enable RX. TX will be enabled when
0078      * there is something to send
0079      */
0080     iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
0081     channel->rx_enable = 1;
0082     return 0;
0083 }
0084 
0085 static int ipoctal_install(struct tty_driver *driver, struct tty_struct *tty)
0086 {
0087     struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
0088     struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
0089     int res;
0090 
0091     if (!ipack_get_carrier(ipoctal->dev))
0092         return -EBUSY;
0093 
0094     res = tty_standard_install(driver, tty);
0095     if (res)
0096         goto err_put_carrier;
0097 
0098     tty->driver_data = channel;
0099 
0100     return 0;
0101 
0102 err_put_carrier:
0103     ipack_put_carrier(ipoctal->dev);
0104 
0105     return res;
0106 }
0107 
0108 static int ipoctal_open(struct tty_struct *tty, struct file *file)
0109 {
0110     struct ipoctal_channel *channel = tty->driver_data;
0111 
0112     return tty_port_open(&channel->tty_port, tty, file);
0113 }
0114 
0115 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
0116 {
0117     stats->tx = 0;
0118     stats->rx = 0;
0119     stats->rcv_break = 0;
0120     stats->framing_err = 0;
0121     stats->overrun_err = 0;
0122     stats->parity_err = 0;
0123 }
0124 
0125 static void ipoctal_free_channel(struct ipoctal_channel *channel)
0126 {
0127     ipoctal_reset_stats(&channel->stats);
0128     channel->pointer_read = 0;
0129     channel->pointer_write = 0;
0130     channel->nb_bytes = 0;
0131 }
0132 
0133 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
0134 {
0135     struct ipoctal_channel *channel = tty->driver_data;
0136 
0137     tty_port_close(&channel->tty_port, tty, filp);
0138     ipoctal_free_channel(channel);
0139 }
0140 
0141 static int ipoctal_get_icount(struct tty_struct *tty,
0142                   struct serial_icounter_struct *icount)
0143 {
0144     struct ipoctal_channel *channel = tty->driver_data;
0145 
0146     icount->cts = 0;
0147     icount->dsr = 0;
0148     icount->rng = 0;
0149     icount->dcd = 0;
0150     icount->rx = channel->stats.rx;
0151     icount->tx = channel->stats.tx;
0152     icount->frame = channel->stats.framing_err;
0153     icount->parity = channel->stats.parity_err;
0154     icount->brk = channel->stats.rcv_break;
0155     return 0;
0156 }
0157 
0158 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
0159 {
0160     struct tty_port *port = &channel->tty_port;
0161     unsigned char value;
0162     unsigned char flag;
0163     u8 isr;
0164 
0165     do {
0166         value = ioread8(&channel->regs->r.rhr);
0167         flag = TTY_NORMAL;
0168         /* Error: count statistics */
0169         if (sr & SR_ERROR) {
0170             iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
0171 
0172             if (sr & SR_OVERRUN_ERROR) {
0173                 channel->stats.overrun_err++;
0174                 /* Overrun doesn't affect the current character*/
0175                 tty_insert_flip_char(port, 0, TTY_OVERRUN);
0176             }
0177             if (sr & SR_PARITY_ERROR) {
0178                 channel->stats.parity_err++;
0179                 flag = TTY_PARITY;
0180             }
0181             if (sr & SR_FRAMING_ERROR) {
0182                 channel->stats.framing_err++;
0183                 flag = TTY_FRAME;
0184             }
0185             if (sr & SR_RECEIVED_BREAK) {
0186                 channel->stats.rcv_break++;
0187                 flag = TTY_BREAK;
0188             }
0189         }
0190         tty_insert_flip_char(port, value, flag);
0191 
0192         /* Check if there are more characters in RX FIFO
0193          * If there are more, the isr register for this channel
0194          * has enabled the RxRDY|FFULL bit.
0195          */
0196         isr = ioread8(&channel->block_regs->r.isr);
0197         sr = ioread8(&channel->regs->r.sr);
0198     } while (isr & channel->isr_rx_rdy_mask);
0199 
0200     tty_flip_buffer_push(port);
0201 }
0202 
0203 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
0204 {
0205     unsigned char value;
0206     unsigned int *pointer_write = &channel->pointer_write;
0207 
0208     if (channel->nb_bytes == 0)
0209         return;
0210 
0211     spin_lock(&channel->lock);
0212     value = channel->tty_port.xmit_buf[*pointer_write];
0213     iowrite8(value, &channel->regs->w.thr);
0214     channel->stats.tx++;
0215     (*pointer_write)++;
0216     *pointer_write = *pointer_write % PAGE_SIZE;
0217     channel->nb_bytes--;
0218     spin_unlock(&channel->lock);
0219 }
0220 
0221 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
0222 {
0223     u8 isr, sr;
0224 
0225     /* The HW is organized in pair of channels.  See which register we need
0226      * to read from */
0227     isr = ioread8(&channel->block_regs->r.isr);
0228     sr = ioread8(&channel->regs->r.sr);
0229 
0230     if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
0231         iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
0232 
0233     if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
0234         iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
0235         /* In case of RS-485, change from TX to RX when finishing TX.
0236          * Half-duplex. */
0237         if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
0238             iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
0239             iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
0240             channel->rx_enable = 1;
0241         }
0242     }
0243 
0244     /* RX data */
0245     if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
0246         ipoctal_irq_rx(channel, sr);
0247 
0248     /* TX of each character */
0249     if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
0250         ipoctal_irq_tx(channel);
0251 }
0252 
0253 static irqreturn_t ipoctal_irq_handler(void *arg)
0254 {
0255     unsigned int i;
0256     struct ipoctal *ipoctal = (struct ipoctal *) arg;
0257 
0258     /* Clear the IPack device interrupt */
0259     readw(ipoctal->int_space + ACK_INT_REQ0);
0260     readw(ipoctal->int_space + ACK_INT_REQ1);
0261 
0262     /* Check all channels */
0263     for (i = 0; i < NR_CHANNELS; i++)
0264         ipoctal_irq_channel(&ipoctal->channel[i]);
0265 
0266     return IRQ_HANDLED;
0267 }
0268 
0269 static const struct tty_port_operations ipoctal_tty_port_ops = {
0270     .dtr_rts = NULL,
0271     .activate = ipoctal_port_activate,
0272 };
0273 
0274 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
0275                  unsigned int slot)
0276 {
0277     int res;
0278     int i;
0279     struct tty_driver *drv;
0280     struct ipoctal_channel *channel;
0281     struct ipack_region *region;
0282     void __iomem *addr;
0283     union scc2698_channel __iomem *chan_regs;
0284     union scc2698_block __iomem *block_regs;
0285 
0286     ipoctal->board_id = ipoctal->dev->id_device;
0287 
0288     region = &ipoctal->dev->region[IPACK_IO_SPACE];
0289     addr = devm_ioremap(&ipoctal->dev->dev,
0290                     region->start, region->size);
0291     if (!addr) {
0292         dev_err(&ipoctal->dev->dev,
0293             "Unable to map slot [%d:%d] IO space!\n",
0294             bus_nr, slot);
0295         return -EADDRNOTAVAIL;
0296     }
0297     /* Save the virtual address to access the registers easily */
0298     chan_regs =
0299         (union scc2698_channel __iomem *) addr;
0300     block_regs =
0301         (union scc2698_block __iomem *) addr;
0302 
0303     region = &ipoctal->dev->region[IPACK_INT_SPACE];
0304     ipoctal->int_space =
0305         devm_ioremap(&ipoctal->dev->dev,
0306                      region->start, region->size);
0307     if (!ipoctal->int_space) {
0308         dev_err(&ipoctal->dev->dev,
0309             "Unable to map slot [%d:%d] INT space!\n",
0310             bus_nr, slot);
0311         return -EADDRNOTAVAIL;
0312     }
0313 
0314     region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
0315     ipoctal->mem8_space =
0316         devm_ioremap(&ipoctal->dev->dev,
0317                      region->start, 0x8000);
0318     if (!ipoctal->mem8_space) {
0319         dev_err(&ipoctal->dev->dev,
0320             "Unable to map slot [%d:%d] MEM8 space!\n",
0321             bus_nr, slot);
0322         return -EADDRNOTAVAIL;
0323     }
0324 
0325 
0326     /* Disable RX and TX before touching anything */
0327     for (i = 0; i < NR_CHANNELS ; i++) {
0328         struct ipoctal_channel *channel = &ipoctal->channel[i];
0329         channel->regs = chan_regs + i;
0330         channel->block_regs = block_regs + (i >> 1);
0331         channel->board_id = ipoctal->board_id;
0332         if (i & 1) {
0333             channel->isr_tx_rdy_mask = ISR_TxRDY_B;
0334             channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
0335         } else {
0336             channel->isr_tx_rdy_mask = ISR_TxRDY_A;
0337             channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
0338         }
0339 
0340         ipoctal_reset_channel(channel);
0341         iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
0342              &channel->regs->w.mr); /* mr1 */
0343         iowrite8(0, &channel->regs->w.mr); /* mr2 */
0344         iowrite8(TX_CLK_9600  | RX_CLK_9600, &channel->regs->w.csr);
0345     }
0346 
0347     for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
0348         iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
0349         iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
0350              &block_regs[i].w.opcr);
0351         iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
0352              IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
0353              &block_regs[i].w.imr);
0354     }
0355 
0356     /* Dummy write */
0357     iowrite8(1, ipoctal->mem8_space + 1);
0358 
0359     /* Register the TTY device */
0360 
0361     /* Each IP-OCTAL channel is a TTY port */
0362     drv = tty_alloc_driver(NR_CHANNELS, TTY_DRIVER_REAL_RAW |
0363             TTY_DRIVER_DYNAMIC_DEV);
0364     if (IS_ERR(drv))
0365         return PTR_ERR(drv);
0366 
0367     /* Fill struct tty_driver with ipoctal data */
0368     drv->owner = THIS_MODULE;
0369     drv->driver_name = KBUILD_MODNAME;
0370     drv->name = kasprintf(GFP_KERNEL, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
0371     if (!drv->name) {
0372         res = -ENOMEM;
0373         goto err_put_driver;
0374     }
0375     drv->major = 0;
0376 
0377     drv->minor_start = 0;
0378     drv->type = TTY_DRIVER_TYPE_SERIAL;
0379     drv->subtype = SERIAL_TYPE_NORMAL;
0380     drv->init_termios = tty_std_termios;
0381     drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
0382     drv->init_termios.c_ispeed = 9600;
0383     drv->init_termios.c_ospeed = 9600;
0384 
0385     tty_set_operations(drv, &ipoctal_fops);
0386     res = tty_register_driver(drv);
0387     if (res) {
0388         dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
0389         goto err_free_name;
0390     }
0391 
0392     /* Save struct tty_driver for use it when uninstalling the device */
0393     ipoctal->tty_drv = drv;
0394 
0395     for (i = 0; i < NR_CHANNELS; i++) {
0396         struct device *tty_dev;
0397 
0398         channel = &ipoctal->channel[i];
0399         tty_port_init(&channel->tty_port);
0400         res = tty_port_alloc_xmit_buf(&channel->tty_port);
0401         if (res)
0402             continue;
0403         channel->tty_port.ops = &ipoctal_tty_port_ops;
0404 
0405         ipoctal_reset_stats(&channel->stats);
0406         channel->nb_bytes = 0;
0407         spin_lock_init(&channel->lock);
0408         channel->pointer_read = 0;
0409         channel->pointer_write = 0;
0410         tty_dev = tty_port_register_device_attr(&channel->tty_port, drv,
0411                             i, NULL, channel, NULL);
0412         if (IS_ERR(tty_dev)) {
0413             dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
0414             tty_port_free_xmit_buf(&channel->tty_port);
0415             tty_port_destroy(&channel->tty_port);
0416             continue;
0417         }
0418         channel->tty_registered = true;
0419     }
0420 
0421     /*
0422      * IP-OCTAL has different addresses to copy its IRQ vector.
0423      * Depending of the carrier these addresses are accesible or not.
0424      * More info in the datasheet.
0425      */
0426     ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
0427                        ipoctal_irq_handler, ipoctal);
0428 
0429     return 0;
0430 
0431 err_free_name:
0432     kfree(drv->name);
0433 err_put_driver:
0434     tty_driver_kref_put(drv);
0435 
0436     return res;
0437 }
0438 
0439 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
0440                         const unsigned char *buf,
0441                         int count)
0442 {
0443     unsigned long flags;
0444     int i;
0445     unsigned int *pointer_read = &channel->pointer_read;
0446 
0447     /* Copy the bytes from the user buffer to the internal one */
0448     for (i = 0; i < count; i++) {
0449         if (i <= (PAGE_SIZE - channel->nb_bytes)) {
0450             spin_lock_irqsave(&channel->lock, flags);
0451             channel->tty_port.xmit_buf[*pointer_read] = buf[i];
0452             *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
0453             channel->nb_bytes++;
0454             spin_unlock_irqrestore(&channel->lock, flags);
0455         } else {
0456             break;
0457         }
0458     }
0459     return i;
0460 }
0461 
0462 static int ipoctal_write_tty(struct tty_struct *tty,
0463                  const unsigned char *buf, int count)
0464 {
0465     struct ipoctal_channel *channel = tty->driver_data;
0466     unsigned int char_copied;
0467 
0468     char_copied = ipoctal_copy_write_buffer(channel, buf, count);
0469 
0470     /* As the IP-OCTAL 485 only supports half duplex, do it manually */
0471     if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
0472         iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
0473         channel->rx_enable = 0;
0474         iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
0475     }
0476 
0477     /*
0478      * Send a packet and then disable TX to avoid failure after several send
0479      * operations
0480      */
0481     iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
0482     return char_copied;
0483 }
0484 
0485 static unsigned int ipoctal_write_room(struct tty_struct *tty)
0486 {
0487     struct ipoctal_channel *channel = tty->driver_data;
0488 
0489     return PAGE_SIZE - channel->nb_bytes;
0490 }
0491 
0492 static unsigned int ipoctal_chars_in_buffer(struct tty_struct *tty)
0493 {
0494     struct ipoctal_channel *channel = tty->driver_data;
0495 
0496     return channel->nb_bytes;
0497 }
0498 
0499 static void ipoctal_set_termios(struct tty_struct *tty,
0500                 struct ktermios *old_termios)
0501 {
0502     unsigned int cflag;
0503     unsigned char mr1 = 0;
0504     unsigned char mr2 = 0;
0505     unsigned char csr = 0;
0506     struct ipoctal_channel *channel = tty->driver_data;
0507     speed_t baud;
0508 
0509     cflag = tty->termios.c_cflag;
0510 
0511     /* Disable and reset everything before change the setup */
0512     ipoctal_reset_channel(channel);
0513 
0514     /* Set Bits per chars */
0515     switch (cflag & CSIZE) {
0516     case CS6:
0517         mr1 |= MR1_CHRL_6_BITS;
0518         break;
0519     case CS7:
0520         mr1 |= MR1_CHRL_7_BITS;
0521         break;
0522     case CS8:
0523     default:
0524         mr1 |= MR1_CHRL_8_BITS;
0525         /* By default, select CS8 */
0526         tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
0527         break;
0528     }
0529 
0530     /* Set Parity */
0531     if (cflag & PARENB)
0532         if (cflag & PARODD)
0533             mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
0534         else
0535             mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
0536     else
0537         mr1 |= MR1_PARITY_OFF;
0538 
0539     /* Mark or space parity is not supported */
0540     tty->termios.c_cflag &= ~CMSPAR;
0541 
0542     /* Set stop bits */
0543     if (cflag & CSTOPB)
0544         mr2 |= MR2_STOP_BITS_LENGTH_2;
0545     else
0546         mr2 |= MR2_STOP_BITS_LENGTH_1;
0547 
0548     /* Set the flow control */
0549     switch (channel->board_id) {
0550     case IPACK1_DEVICE_ID_SBS_OCTAL_232:
0551         if (cflag & CRTSCTS) {
0552             mr1 |= MR1_RxRTS_CONTROL_ON;
0553             mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
0554         } else {
0555             mr1 |= MR1_RxRTS_CONTROL_OFF;
0556             mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
0557         }
0558         break;
0559     case IPACK1_DEVICE_ID_SBS_OCTAL_422:
0560         mr1 |= MR1_RxRTS_CONTROL_OFF;
0561         mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
0562         break;
0563     case IPACK1_DEVICE_ID_SBS_OCTAL_485:
0564         mr1 |= MR1_RxRTS_CONTROL_OFF;
0565         mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
0566         break;
0567     default:
0568         return;
0569     }
0570 
0571     baud = tty_get_baud_rate(tty);
0572     tty_termios_encode_baud_rate(&tty->termios, baud, baud);
0573 
0574     /* Set baud rate */
0575     switch (baud) {
0576     case 75:
0577         csr |= TX_CLK_75 | RX_CLK_75;
0578         break;
0579     case 110:
0580         csr |= TX_CLK_110 | RX_CLK_110;
0581         break;
0582     case 150:
0583         csr |= TX_CLK_150 | RX_CLK_150;
0584         break;
0585     case 300:
0586         csr |= TX_CLK_300 | RX_CLK_300;
0587         break;
0588     case 600:
0589         csr |= TX_CLK_600 | RX_CLK_600;
0590         break;
0591     case 1200:
0592         csr |= TX_CLK_1200 | RX_CLK_1200;
0593         break;
0594     case 1800:
0595         csr |= TX_CLK_1800 | RX_CLK_1800;
0596         break;
0597     case 2000:
0598         csr |= TX_CLK_2000 | RX_CLK_2000;
0599         break;
0600     case 2400:
0601         csr |= TX_CLK_2400 | RX_CLK_2400;
0602         break;
0603     case 4800:
0604         csr |= TX_CLK_4800  | RX_CLK_4800;
0605         break;
0606     case 9600:
0607         csr |= TX_CLK_9600  | RX_CLK_9600;
0608         break;
0609     case 19200:
0610         csr |= TX_CLK_19200 | RX_CLK_19200;
0611         break;
0612     case 38400:
0613     default:
0614         csr |= TX_CLK_38400 | RX_CLK_38400;
0615         /* In case of default, we establish 38400 bps */
0616         tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
0617         break;
0618     }
0619 
0620     mr1 |= MR1_ERROR_CHAR;
0621     mr1 |= MR1_RxINT_RxRDY;
0622 
0623     /* Write the control registers */
0624     iowrite8(mr1, &channel->regs->w.mr);
0625     iowrite8(mr2, &channel->regs->w.mr);
0626     iowrite8(csr, &channel->regs->w.csr);
0627 
0628     /* Enable again the RX, if it was before */
0629     if (channel->rx_enable)
0630         iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
0631 }
0632 
0633 static void ipoctal_hangup(struct tty_struct *tty)
0634 {
0635     unsigned long flags;
0636     struct ipoctal_channel *channel = tty->driver_data;
0637 
0638     if (channel == NULL)
0639         return;
0640 
0641     spin_lock_irqsave(&channel->lock, flags);
0642     channel->nb_bytes = 0;
0643     channel->pointer_read = 0;
0644     channel->pointer_write = 0;
0645     spin_unlock_irqrestore(&channel->lock, flags);
0646 
0647     tty_port_hangup(&channel->tty_port);
0648 
0649     ipoctal_reset_channel(channel);
0650     tty_port_set_initialized(&channel->tty_port, 0);
0651     wake_up_interruptible(&channel->tty_port.open_wait);
0652 }
0653 
0654 static void ipoctal_shutdown(struct tty_struct *tty)
0655 {
0656     struct ipoctal_channel *channel = tty->driver_data;
0657 
0658     if (channel == NULL)
0659         return;
0660 
0661     ipoctal_reset_channel(channel);
0662     tty_port_set_initialized(&channel->tty_port, 0);
0663 }
0664 
0665 static void ipoctal_cleanup(struct tty_struct *tty)
0666 {
0667     struct ipoctal_channel *channel = tty->driver_data;
0668     struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
0669 
0670     /* release the carrier driver */
0671     ipack_put_carrier(ipoctal->dev);
0672 }
0673 
0674 static const struct tty_operations ipoctal_fops = {
0675     .ioctl =        NULL,
0676     .install =      ipoctal_install,
0677     .open =         ipoctal_open,
0678     .close =        ipoctal_close,
0679     .write =        ipoctal_write_tty,
0680     .set_termios =      ipoctal_set_termios,
0681     .write_room =       ipoctal_write_room,
0682     .chars_in_buffer =  ipoctal_chars_in_buffer,
0683     .get_icount =       ipoctal_get_icount,
0684     .hangup =       ipoctal_hangup,
0685     .shutdown =     ipoctal_shutdown,
0686     .cleanup =              ipoctal_cleanup,
0687 };
0688 
0689 static int ipoctal_probe(struct ipack_device *dev)
0690 {
0691     int res;
0692     struct ipoctal *ipoctal;
0693 
0694     ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
0695     if (ipoctal == NULL)
0696         return -ENOMEM;
0697 
0698     ipoctal->dev = dev;
0699     res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
0700     if (res)
0701         goto out_uninst;
0702 
0703     dev_set_drvdata(&dev->dev, ipoctal);
0704     return 0;
0705 
0706 out_uninst:
0707     kfree(ipoctal);
0708     return res;
0709 }
0710 
0711 static void __ipoctal_remove(struct ipoctal *ipoctal)
0712 {
0713     int i;
0714 
0715     ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
0716 
0717     for (i = 0; i < NR_CHANNELS; i++) {
0718         struct ipoctal_channel *channel = &ipoctal->channel[i];
0719 
0720         if (!channel->tty_registered)
0721             continue;
0722 
0723         tty_unregister_device(ipoctal->tty_drv, i);
0724         tty_port_free_xmit_buf(&channel->tty_port);
0725         tty_port_destroy(&channel->tty_port);
0726     }
0727 
0728     tty_unregister_driver(ipoctal->tty_drv);
0729     kfree(ipoctal->tty_drv->name);
0730     tty_driver_kref_put(ipoctal->tty_drv);
0731     kfree(ipoctal);
0732 }
0733 
0734 static void ipoctal_remove(struct ipack_device *idev)
0735 {
0736     __ipoctal_remove(dev_get_drvdata(&idev->dev));
0737 }
0738 
0739 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
0740     { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
0741             IPACK1_DEVICE_ID_SBS_OCTAL_232) },
0742     { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
0743             IPACK1_DEVICE_ID_SBS_OCTAL_422) },
0744     { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
0745             IPACK1_DEVICE_ID_SBS_OCTAL_485) },
0746     { 0, },
0747 };
0748 
0749 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
0750 
0751 static const struct ipack_driver_ops ipoctal_drv_ops = {
0752     .probe  = ipoctal_probe,
0753     .remove = ipoctal_remove,
0754 };
0755 
0756 static struct ipack_driver driver = {
0757     .ops      = &ipoctal_drv_ops,
0758     .id_table = ipoctal_ids,
0759 };
0760 
0761 static int __init ipoctal_init(void)
0762 {
0763     return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
0764 }
0765 
0766 static void __exit ipoctal_exit(void)
0767 {
0768     ipack_driver_unregister(&driver);
0769 }
0770 
0771 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
0772 MODULE_LICENSE("GPL");
0773 
0774 module_init(ipoctal_init);
0775 module_exit(ipoctal_exit);