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