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