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