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