]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/ipack/devices/ipoctal.c
ipoctal: get carrier driver to avoid rmmod
[karo-tx-linux.git] / drivers / ipack / devices / ipoctal.c
1 /**
2  * ipoctal.c
3  *
4  * driver for the GE IP-OCTAL boards
5  *
6  * Copyright (C) 2009-2012 CERN (www.cern.ch)
7  * Author: Nicolas Serafini, EIC2 SA
8  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; version 2 of the License.
13  */
14
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/sched.h>
19 #include <linux/tty.h>
20 #include <linux/serial.h>
21 #include <linux/tty_flip.h>
22 #include <linux/slab.h>
23 #include <linux/io.h>
24 #include <linux/ipack.h>
25 #include "ipoctal.h"
26 #include "scc2698.h"
27
28 #define IP_OCTAL_ID_SPACE_VECTOR    0x41
29 #define IP_OCTAL_NB_BLOCKS          4
30
31 static const struct tty_operations ipoctal_fops;
32
33 struct ipoctal_channel {
34         struct ipoctal_stats            stats;
35         unsigned int                    nb_bytes;
36         wait_queue_head_t               queue;
37         spinlock_t                      lock;
38         unsigned int                    pointer_read;
39         unsigned int                    pointer_write;
40         struct tty_port                 tty_port;
41         union scc2698_channel __iomem   *regs;
42         union scc2698_block __iomem     *block_regs;
43         unsigned int                    board_id;
44         u8                              isr_rx_rdy_mask;
45         u8                              isr_tx_rdy_mask;
46         unsigned int                    rx_enable;
47 };
48
49 struct ipoctal {
50         struct ipack_device             *dev;
51         unsigned int                    board_id;
52         struct ipoctal_channel          channel[NR_CHANNELS];
53         struct tty_driver               *tty_drv;
54         u8 __iomem                      *mem8_space;
55         u8 __iomem                      *int_space;
56 };
57
58 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
59                                               unsigned int index)
60 {
61         return container_of(chan, struct ipoctal, channel[index]);
62 }
63
64 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
65 {
66         iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
67         channel->rx_enable = 0;
68         iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
69         iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
70         iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
71         iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
72 }
73
74 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
75 {
76         struct ipoctal_channel *channel;
77
78         channel = dev_get_drvdata(tty->dev);
79
80         /*
81          * Enable RX. TX will be enabled when
82          * there is something to send
83          */
84         iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
85         channel->rx_enable = 1;
86         return 0;
87 }
88
89 static int ipoctal_open(struct tty_struct *tty, struct file *file)
90 {
91         struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
92         struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
93         int err;
94
95         tty->driver_data = channel;
96
97         if (!ipack_get_carrier(ipoctal->dev))
98                 return -EBUSY;
99
100         err = tty_port_open(&channel->tty_port, tty, file);
101         if (err)
102                 ipack_put_carrier(ipoctal->dev);
103
104         return err;
105 }
106
107 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
108 {
109         stats->tx = 0;
110         stats->rx = 0;
111         stats->rcv_break = 0;
112         stats->framing_err = 0;
113         stats->overrun_err = 0;
114         stats->parity_err = 0;
115 }
116
117 static void ipoctal_free_channel(struct ipoctal_channel *channel)
118 {
119         ipoctal_reset_stats(&channel->stats);
120         channel->pointer_read = 0;
121         channel->pointer_write = 0;
122         channel->nb_bytes = 0;
123 }
124
125 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
126 {
127         struct ipoctal_channel *channel = tty->driver_data;
128
129         tty_port_close(&channel->tty_port, tty, filp);
130         ipoctal_free_channel(channel);
131 }
132
133 static int ipoctal_get_icount(struct tty_struct *tty,
134                               struct serial_icounter_struct *icount)
135 {
136         struct ipoctal_channel *channel = tty->driver_data;
137
138         icount->cts = 0;
139         icount->dsr = 0;
140         icount->rng = 0;
141         icount->dcd = 0;
142         icount->rx = channel->stats.rx;
143         icount->tx = channel->stats.tx;
144         icount->frame = channel->stats.framing_err;
145         icount->parity = channel->stats.parity_err;
146         icount->brk = channel->stats.rcv_break;
147         return 0;
148 }
149
150 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
151 {
152         struct tty_port *port = &channel->tty_port;
153         unsigned char value;
154         unsigned char flag;
155         u8 isr;
156
157         do {
158                 value = ioread8(&channel->regs->r.rhr);
159                 flag = TTY_NORMAL;
160                 /* Error: count statistics */
161                 if (sr & SR_ERROR) {
162                         iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
163
164                         if (sr & SR_OVERRUN_ERROR) {
165                                 channel->stats.overrun_err++;
166                                 /* Overrun doesn't affect the current character*/
167                                 tty_insert_flip_char(port, 0, TTY_OVERRUN);
168                         }
169                         if (sr & SR_PARITY_ERROR) {
170                                 channel->stats.parity_err++;
171                                 flag = TTY_PARITY;
172                         }
173                         if (sr & SR_FRAMING_ERROR) {
174                                 channel->stats.framing_err++;
175                                 flag = TTY_FRAME;
176                         }
177                         if (sr & SR_RECEIVED_BREAK) {
178                                 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
179                                 channel->stats.rcv_break++;
180                                 flag = TTY_BREAK;
181                         }
182                 }
183                 tty_insert_flip_char(port, value, flag);
184
185                 /* Check if there are more characters in RX FIFO
186                  * If there are more, the isr register for this channel
187                  * has enabled the RxRDY|FFULL bit.
188                  */
189                 isr = ioread8(&channel->block_regs->r.isr);
190                 sr = ioread8(&channel->regs->r.sr);
191         } while (isr & channel->isr_rx_rdy_mask);
192
193         tty_flip_buffer_push(port);
194 }
195
196 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
197 {
198         unsigned char value;
199         unsigned int *pointer_write = &channel->pointer_write;
200
201         if (channel->nb_bytes == 0)
202                 return;
203
204         spin_lock(&channel->lock);
205         value = channel->tty_port.xmit_buf[*pointer_write];
206         iowrite8(value, &channel->regs->w.thr);
207         channel->stats.tx++;
208         (*pointer_write)++;
209         *pointer_write = *pointer_write % PAGE_SIZE;
210         channel->nb_bytes--;
211         spin_unlock(&channel->lock);
212 }
213
214 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
215 {
216         u8 isr, sr;
217
218         /* The HW is organized in pair of channels.  See which register we need
219          * to read from */
220         isr = ioread8(&channel->block_regs->r.isr);
221         sr = ioread8(&channel->regs->r.sr);
222
223         if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
224                 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
225                 /* In case of RS-485, change from TX to RX when finishing TX.
226                  * Half-duplex. */
227                 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
228                         iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
229                         iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
230                         channel->rx_enable = 1;
231                 }
232         }
233
234         /* RX data */
235         if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
236                 ipoctal_irq_rx(channel, sr);
237
238         /* TX of each character */
239         if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
240                 ipoctal_irq_tx(channel);
241 }
242
243 static irqreturn_t ipoctal_irq_handler(void *arg)
244 {
245         unsigned int i;
246         struct ipoctal *ipoctal = (struct ipoctal *) arg;
247
248         /* Clear the IPack device interrupt */
249         readw(ipoctal->int_space + ACK_INT_REQ0);
250         readw(ipoctal->int_space + ACK_INT_REQ1);
251
252         /* Check all channels */
253         for (i = 0; i < NR_CHANNELS; i++)
254                 ipoctal_irq_channel(&ipoctal->channel[i]);
255
256         return IRQ_HANDLED;
257 }
258
259 static const struct tty_port_operations ipoctal_tty_port_ops = {
260         .dtr_rts = NULL,
261         .activate = ipoctal_port_activate,
262 };
263
264 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
265                              unsigned int slot)
266 {
267         int res;
268         int i;
269         struct tty_driver *tty;
270         char name[20];
271         struct ipoctal_channel *channel;
272         struct ipack_region *region;
273         void __iomem *addr;
274         union scc2698_channel __iomem *chan_regs;
275         union scc2698_block __iomem *block_regs;
276
277         ipoctal->board_id = ipoctal->dev->id_device;
278
279         region = &ipoctal->dev->region[IPACK_IO_SPACE];
280         addr = devm_ioremap_nocache(&ipoctal->dev->dev,
281                                     region->start, region->size);
282         if (!addr) {
283                 dev_err(&ipoctal->dev->dev,
284                         "Unable to map slot [%d:%d] IO space!\n",
285                         bus_nr, slot);
286                 return -EADDRNOTAVAIL;
287         }
288         /* Save the virtual address to access the registers easily */
289         chan_regs =
290                 (union scc2698_channel __iomem *) addr;
291         block_regs =
292                 (union scc2698_block __iomem *) addr;
293
294         region = &ipoctal->dev->region[IPACK_INT_SPACE];
295         ipoctal->int_space =
296                 devm_ioremap_nocache(&ipoctal->dev->dev,
297                                      region->start, region->size);
298         if (!ipoctal->int_space) {
299                 dev_err(&ipoctal->dev->dev,
300                         "Unable to map slot [%d:%d] INT space!\n",
301                         bus_nr, slot);
302                 return -EADDRNOTAVAIL;
303         }
304
305         region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
306         ipoctal->mem8_space =
307                 devm_ioremap_nocache(&ipoctal->dev->dev,
308                                      region->start, 0x8000);
309         if (!ipoctal->mem8_space) {
310                 dev_err(&ipoctal->dev->dev,
311                         "Unable to map slot [%d:%d] MEM8 space!\n",
312                         bus_nr, slot);
313                 return -EADDRNOTAVAIL;
314         }
315
316
317         /* Disable RX and TX before touching anything */
318         for (i = 0; i < NR_CHANNELS ; i++) {
319                 struct ipoctal_channel *channel = &ipoctal->channel[i];
320                 channel->regs = chan_regs + i;
321                 channel->block_regs = block_regs + (i >> 1);
322                 channel->board_id = ipoctal->board_id;
323                 if (i & 1) {
324                         channel->isr_tx_rdy_mask = ISR_TxRDY_B;
325                         channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
326                 } else {
327                         channel->isr_tx_rdy_mask = ISR_TxRDY_A;
328                         channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
329                 }
330
331                 ipoctal_reset_channel(channel);
332                 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
333                          &channel->regs->w.mr); /* mr1 */
334                 iowrite8(0, &channel->regs->w.mr); /* mr2 */
335                 iowrite8(TX_CLK_9600  | RX_CLK_9600, &channel->regs->w.csr);
336         }
337
338         for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
339                 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
340                 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
341                          &block_regs[i].w.opcr);
342                 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
343                          IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
344                          &block_regs[i].w.imr);
345         }
346
347         /* Dummy write */
348         iowrite8(1, ipoctal->mem8_space + 1);
349
350         /* Register the TTY device */
351
352         /* Each IP-OCTAL channel is a TTY port */
353         tty = alloc_tty_driver(NR_CHANNELS);
354
355         if (!tty)
356                 return -ENOMEM;
357
358         /* Fill struct tty_driver with ipoctal data */
359         tty->owner = THIS_MODULE;
360         tty->driver_name = KBUILD_MODNAME;
361         sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
362         tty->name = name;
363         tty->major = 0;
364
365         tty->minor_start = 0;
366         tty->type = TTY_DRIVER_TYPE_SERIAL;
367         tty->subtype = SERIAL_TYPE_NORMAL;
368         tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
369         tty->init_termios = tty_std_termios;
370         tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
371         tty->init_termios.c_ispeed = 9600;
372         tty->init_termios.c_ospeed = 9600;
373
374         tty_set_operations(tty, &ipoctal_fops);
375         res = tty_register_driver(tty);
376         if (res) {
377                 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
378                 put_tty_driver(tty);
379                 return res;
380         }
381
382         /* Save struct tty_driver for use it when uninstalling the device */
383         ipoctal->tty_drv = tty;
384
385         for (i = 0; i < NR_CHANNELS; i++) {
386                 struct device *tty_dev;
387
388                 channel = &ipoctal->channel[i];
389                 tty_port_init(&channel->tty_port);
390                 tty_port_alloc_xmit_buf(&channel->tty_port);
391                 channel->tty_port.ops = &ipoctal_tty_port_ops;
392
393                 ipoctal_reset_stats(&channel->stats);
394                 channel->nb_bytes = 0;
395                 spin_lock_init(&channel->lock);
396                 channel->pointer_read = 0;
397                 channel->pointer_write = 0;
398                 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
399                 if (IS_ERR(tty_dev)) {
400                         dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
401                         tty_port_destroy(&channel->tty_port);
402                         continue;
403                 }
404                 dev_set_drvdata(tty_dev, channel);
405         }
406
407         /*
408          * IP-OCTAL has different addresses to copy its IRQ vector.
409          * Depending of the carrier these addresses are accesible or not.
410          * More info in the datasheet.
411          */
412         ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
413                                        ipoctal_irq_handler, ipoctal);
414
415         return 0;
416 }
417
418 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
419                                             const unsigned char *buf,
420                                             int count)
421 {
422         unsigned long flags;
423         int i;
424         unsigned int *pointer_read = &channel->pointer_read;
425
426         /* Copy the bytes from the user buffer to the internal one */
427         for (i = 0; i < count; i++) {
428                 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
429                         spin_lock_irqsave(&channel->lock, flags);
430                         channel->tty_port.xmit_buf[*pointer_read] = buf[i];
431                         *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
432                         channel->nb_bytes++;
433                         spin_unlock_irqrestore(&channel->lock, flags);
434                 } else {
435                         break;
436                 }
437         }
438         return i;
439 }
440
441 static int ipoctal_write_tty(struct tty_struct *tty,
442                              const unsigned char *buf, int count)
443 {
444         struct ipoctal_channel *channel = tty->driver_data;
445         unsigned int char_copied;
446
447         char_copied = ipoctal_copy_write_buffer(channel, buf, count);
448
449         /* As the IP-OCTAL 485 only supports half duplex, do it manually */
450         if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
451                 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
452                 channel->rx_enable = 0;
453                 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
454         }
455
456         /*
457          * Send a packet and then disable TX to avoid failure after several send
458          * operations
459          */
460         iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
461         return char_copied;
462 }
463
464 static int ipoctal_write_room(struct tty_struct *tty)
465 {
466         struct ipoctal_channel *channel = tty->driver_data;
467
468         return PAGE_SIZE - channel->nb_bytes;
469 }
470
471 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
472 {
473         struct ipoctal_channel *channel = tty->driver_data;
474
475         return channel->nb_bytes;
476 }
477
478 static void ipoctal_set_termios(struct tty_struct *tty,
479                                 struct ktermios *old_termios)
480 {
481         unsigned int cflag;
482         unsigned char mr1 = 0;
483         unsigned char mr2 = 0;
484         unsigned char csr = 0;
485         struct ipoctal_channel *channel = tty->driver_data;
486         speed_t baud;
487
488         cflag = tty->termios.c_cflag;
489
490         /* Disable and reset everything before change the setup */
491         ipoctal_reset_channel(channel);
492
493         /* Set Bits per chars */
494         switch (cflag & CSIZE) {
495         case CS6:
496                 mr1 |= MR1_CHRL_6_BITS;
497                 break;
498         case CS7:
499                 mr1 |= MR1_CHRL_7_BITS;
500                 break;
501         case CS8:
502         default:
503                 mr1 |= MR1_CHRL_8_BITS;
504                 /* By default, select CS8 */
505                 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
506                 break;
507         }
508
509         /* Set Parity */
510         if (cflag & PARENB)
511                 if (cflag & PARODD)
512                         mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
513                 else
514                         mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
515         else
516                 mr1 |= MR1_PARITY_OFF;
517
518         /* Mark or space parity is not supported */
519         tty->termios.c_cflag &= ~CMSPAR;
520
521         /* Set stop bits */
522         if (cflag & CSTOPB)
523                 mr2 |= MR2_STOP_BITS_LENGTH_2;
524         else
525                 mr2 |= MR2_STOP_BITS_LENGTH_1;
526
527         /* Set the flow control */
528         switch (channel->board_id) {
529         case IPACK1_DEVICE_ID_SBS_OCTAL_232:
530                 if (cflag & CRTSCTS) {
531                         mr1 |= MR1_RxRTS_CONTROL_ON;
532                         mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
533                 } else {
534                         mr1 |= MR1_RxRTS_CONTROL_OFF;
535                         mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
536                 }
537                 break;
538         case IPACK1_DEVICE_ID_SBS_OCTAL_422:
539                 mr1 |= MR1_RxRTS_CONTROL_OFF;
540                 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
541                 break;
542         case IPACK1_DEVICE_ID_SBS_OCTAL_485:
543                 mr1 |= MR1_RxRTS_CONTROL_OFF;
544                 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
545                 break;
546         default:
547                 return;
548                 break;
549         }
550
551         baud = tty_get_baud_rate(tty);
552         tty_termios_encode_baud_rate(&tty->termios, baud, baud);
553
554         /* Set baud rate */
555         switch (baud) {
556         case 75:
557                 csr |= TX_CLK_75 | RX_CLK_75;
558                 break;
559         case 110:
560                 csr |= TX_CLK_110 | RX_CLK_110;
561                 break;
562         case 150:
563                 csr |= TX_CLK_150 | RX_CLK_150;
564                 break;
565         case 300:
566                 csr |= TX_CLK_300 | RX_CLK_300;
567                 break;
568         case 600:
569                 csr |= TX_CLK_600 | RX_CLK_600;
570                 break;
571         case 1200:
572                 csr |= TX_CLK_1200 | RX_CLK_1200;
573                 break;
574         case 1800:
575                 csr |= TX_CLK_1800 | RX_CLK_1800;
576                 break;
577         case 2000:
578                 csr |= TX_CLK_2000 | RX_CLK_2000;
579                 break;
580         case 2400:
581                 csr |= TX_CLK_2400 | RX_CLK_2400;
582                 break;
583         case 4800:
584                 csr |= TX_CLK_4800  | RX_CLK_4800;
585                 break;
586         case 9600:
587                 csr |= TX_CLK_9600  | RX_CLK_9600;
588                 break;
589         case 19200:
590                 csr |= TX_CLK_19200 | RX_CLK_19200;
591                 break;
592         case 38400:
593         default:
594                 csr |= TX_CLK_38400 | RX_CLK_38400;
595                 /* In case of default, we establish 38400 bps */
596                 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
597                 break;
598         }
599
600         mr1 |= MR1_ERROR_CHAR;
601         mr1 |= MR1_RxINT_RxRDY;
602
603         /* Write the control registers */
604         iowrite8(mr1, &channel->regs->w.mr);
605         iowrite8(mr2, &channel->regs->w.mr);
606         iowrite8(csr, &channel->regs->w.csr);
607
608         /* Enable again the RX, if it was before */
609         if (channel->rx_enable)
610                 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
611 }
612
613 static void ipoctal_hangup(struct tty_struct *tty)
614 {
615         unsigned long flags;
616         struct ipoctal_channel *channel = tty->driver_data;
617
618         if (channel == NULL)
619                 return;
620
621         spin_lock_irqsave(&channel->lock, flags);
622         channel->nb_bytes = 0;
623         channel->pointer_read = 0;
624         channel->pointer_write = 0;
625         spin_unlock_irqrestore(&channel->lock, flags);
626
627         tty_port_hangup(&channel->tty_port);
628
629         ipoctal_reset_channel(channel);
630
631         clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
632         wake_up_interruptible(&channel->tty_port.open_wait);
633 }
634
635 static void ipoctal_shutdown(struct tty_struct *tty)
636 {
637         struct ipoctal_channel *channel = tty->driver_data;
638
639         if (channel == NULL)
640                 return;
641
642         ipoctal_reset_channel(channel);
643         clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
644 }
645
646 static void ipoctal_cleanup(struct tty_struct *tty)
647 {
648         struct ipoctal_channel *channel = tty->driver_data;
649         struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
650
651         /* release the carrier driver */
652         ipack_put_carrier(ipoctal->dev);
653 }
654
655 static const struct tty_operations ipoctal_fops = {
656         .ioctl =                NULL,
657         .open =                 ipoctal_open,
658         .close =                ipoctal_close,
659         .write =                ipoctal_write_tty,
660         .set_termios =          ipoctal_set_termios,
661         .write_room =           ipoctal_write_room,
662         .chars_in_buffer =      ipoctal_chars_in_buffer,
663         .get_icount =           ipoctal_get_icount,
664         .hangup =               ipoctal_hangup,
665         .shutdown =             ipoctal_shutdown,
666         .cleanup =              ipoctal_cleanup,
667 };
668
669 static int ipoctal_probe(struct ipack_device *dev)
670 {
671         int res;
672         struct ipoctal *ipoctal;
673
674         ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
675         if (ipoctal == NULL)
676                 return -ENOMEM;
677
678         ipoctal->dev = dev;
679         res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
680         if (res)
681                 goto out_uninst;
682
683         dev_set_drvdata(&dev->dev, ipoctal);
684         return 0;
685
686 out_uninst:
687         kfree(ipoctal);
688         return res;
689 }
690
691 static void __ipoctal_remove(struct ipoctal *ipoctal)
692 {
693         int i;
694
695         ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
696
697         for (i = 0; i < NR_CHANNELS; i++) {
698                 struct ipoctal_channel *channel = &ipoctal->channel[i];
699                 tty_unregister_device(ipoctal->tty_drv, i);
700                 tty_port_free_xmit_buf(&channel->tty_port);
701                 tty_port_destroy(&channel->tty_port);
702         }
703
704         tty_unregister_driver(ipoctal->tty_drv);
705         put_tty_driver(ipoctal->tty_drv);
706         kfree(ipoctal);
707 }
708
709 static void ipoctal_remove(struct ipack_device *idev)
710 {
711         __ipoctal_remove(dev_get_drvdata(&idev->dev));
712 }
713
714 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
715         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
716                         IPACK1_DEVICE_ID_SBS_OCTAL_232) },
717         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
718                         IPACK1_DEVICE_ID_SBS_OCTAL_422) },
719         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
720                         IPACK1_DEVICE_ID_SBS_OCTAL_485) },
721         { 0, },
722 };
723
724 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
725
726 static const struct ipack_driver_ops ipoctal_drv_ops = {
727         .probe  = ipoctal_probe,
728         .remove = ipoctal_remove,
729 };
730
731 static struct ipack_driver driver = {
732         .ops      = &ipoctal_drv_ops,
733         .id_table = ipoctal_ids,
734 };
735
736 static int __init ipoctal_init(void)
737 {
738         return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
739 }
740
741 static void __exit ipoctal_exit(void)
742 {
743         ipack_driver_unregister(&driver);
744 }
745
746 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
747 MODULE_LICENSE("GPL");
748
749 module_init(ipoctal_init);
750 module_exit(ipoctal_exit);