Staging: ipack/devices/ipoctal: Split interrupt service routine.
[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 int 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(struct ipoctal_channel *channel,
497                          const unsigned char *buf, int count)
498 {
499         channel->nb_bytes = 0;
500         channel->count_wr = 0;
501
502         ipoctal_copy_write_buffer(channel, buf, count);
503
504         /* As the IP-OCTAL 485 only supports half duplex, do it manually */
505         if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
506                 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
507                 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
508         }
509
510         /*
511          * Send a packet and then disable TX to avoid failure after several send
512          * operations
513          */
514         iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
515         wait_event_interruptible(channel->queue, *channel->board_write);
516         iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
517
518         *channel->board_write = 0;
519         return channel->count_wr;
520 }
521
522 static int ipoctal_write_tty(struct tty_struct *tty,
523                              const unsigned char *buf, int count)
524 {
525         struct ipoctal_channel *channel = tty->driver_data;
526
527         return ipoctal_write(channel, buf, count);
528 }
529
530 static int ipoctal_write_room(struct tty_struct *tty)
531 {
532         struct ipoctal_channel *channel = tty->driver_data;
533
534         return PAGE_SIZE - channel->nb_bytes;
535 }
536
537 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
538 {
539         struct ipoctal_channel *channel = tty->driver_data;
540
541         return channel->nb_bytes;
542 }
543
544 static void ipoctal_set_termios(struct tty_struct *tty,
545                                 struct ktermios *old_termios)
546 {
547         unsigned int cflag;
548         unsigned char mr1 = 0;
549         unsigned char mr2 = 0;
550         unsigned char csr = 0;
551         struct ipoctal_channel *channel = tty->driver_data;
552         speed_t baud;
553
554         cflag = tty->termios->c_cflag;
555
556         /* Disable and reset everything before change the setup */
557         iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
558         iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
559         iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
560         iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
561         iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
562
563         /* Set Bits per chars */
564         switch (cflag & CSIZE) {
565         case CS6:
566                 mr1 |= MR1_CHRL_6_BITS;
567                 break;
568         case CS7:
569                 mr1 |= MR1_CHRL_7_BITS;
570                 break;
571         case CS8:
572         default:
573                 mr1 |= MR1_CHRL_8_BITS;
574                 /* By default, select CS8 */
575                 tty->termios->c_cflag = (cflag & ~CSIZE) | CS8;
576                 break;
577         }
578
579         /* Set Parity */
580         if (cflag & PARENB)
581                 if (cflag & PARODD)
582                         mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
583                 else
584                         mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
585         else
586                 mr1 |= MR1_PARITY_OFF;
587
588         /* Mark or space parity is not supported */
589         tty->termios->c_cflag &= ~CMSPAR;
590
591         /* Set stop bits */
592         if (cflag & CSTOPB)
593                 mr2 |= MR2_STOP_BITS_LENGTH_2;
594         else
595                 mr2 |= MR2_STOP_BITS_LENGTH_1;
596
597         /* Set the flow control */
598         switch (channel->board_id) {
599         case IPACK1_DEVICE_ID_SBS_OCTAL_232:
600                 if (cflag & CRTSCTS) {
601                         mr1 |= MR1_RxRTS_CONTROL_ON;
602                         mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
603                 } else {
604                         mr1 |= MR1_RxRTS_CONTROL_OFF;
605                         mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
606                 }
607                 break;
608         case IPACK1_DEVICE_ID_SBS_OCTAL_422:
609                 mr1 |= MR1_RxRTS_CONTROL_OFF;
610                 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
611                 break;
612         case IPACK1_DEVICE_ID_SBS_OCTAL_485:
613                 mr1 |= MR1_RxRTS_CONTROL_OFF;
614                 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
615                 break;
616         default:
617                 return;
618                 break;
619         }
620
621         baud = tty_get_baud_rate(tty);
622         tty_termios_encode_baud_rate(tty->termios, baud, baud);
623
624         /* Set baud rate */
625         switch (tty->termios->c_ospeed) {
626         case 75:
627                 csr |= TX_CLK_75 | RX_CLK_75;
628                 break;
629         case 110:
630                 csr |= TX_CLK_110 | RX_CLK_110;
631                 break;
632         case 150:
633                 csr |= TX_CLK_150 | RX_CLK_150;
634                 break;
635         case 300:
636                 csr |= TX_CLK_300 | RX_CLK_300;
637                 break;
638         case 600:
639                 csr |= TX_CLK_600 | RX_CLK_600;
640                 break;
641         case 1200:
642                 csr |= TX_CLK_1200 | RX_CLK_1200;
643                 break;
644         case 1800:
645                 csr |= TX_CLK_1800 | RX_CLK_1800;
646                 break;
647         case 2000:
648                 csr |= TX_CLK_2000 | RX_CLK_2000;
649                 break;
650         case 2400:
651                 csr |= TX_CLK_2400 | RX_CLK_2400;
652                 break;
653         case 4800:
654                 csr |= TX_CLK_4800  | RX_CLK_4800;
655                 break;
656         case 9600:
657                 csr |= TX_CLK_9600  | RX_CLK_9600;
658                 break;
659         case 19200:
660                 csr |= TX_CLK_19200 | RX_CLK_19200;
661                 break;
662         case 38400:
663         default:
664                 csr |= TX_CLK_38400 | RX_CLK_38400;
665                 /* In case of default, we establish 38400 bps */
666                 tty_termios_encode_baud_rate(tty->termios, 38400, 38400);
667                 break;
668         }
669
670         mr1 |= MR1_ERROR_CHAR;
671         mr1 |= MR1_RxINT_RxRDY;
672
673         /* Write the control registers */
674         iowrite8(mr1, &channel->regs->w.mr);
675         iowrite8(mr2, &channel->regs->w.mr);
676         iowrite8(csr, &channel->regs->w.csr);
677
678         /* Enable again the RX */
679         iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
680 }
681
682 static void ipoctal_hangup(struct tty_struct *tty)
683 {
684         unsigned long flags;
685         struct ipoctal_channel *channel = tty->driver_data;
686
687         if (channel == NULL)
688                 return;
689
690         spin_lock_irqsave(&channel->lock, flags);
691         channel->nb_bytes = 0;
692         channel->pointer_read = 0;
693         channel->pointer_write = 0;
694         spin_unlock_irqrestore(&channel->lock, flags);
695
696         tty_port_hangup(&channel->tty_port);
697
698         iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
699         iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
700         iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
701         iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
702         iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
703
704         clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
705         wake_up_interruptible(&channel->tty_port.open_wait);
706 }
707
708 static const struct tty_operations ipoctal_fops = {
709         .ioctl =                NULL,
710         .open =                 ipoctal_open,
711         .close =                ipoctal_close,
712         .write =                ipoctal_write_tty,
713         .set_termios =          ipoctal_set_termios,
714         .write_room =           ipoctal_write_room,
715         .chars_in_buffer =      ipoctal_chars_in_buffer,
716         .get_icount =           ipoctal_get_icount,
717         .hangup =               ipoctal_hangup,
718 };
719
720 static int ipoctal_probe(struct ipack_device *dev)
721 {
722         int res;
723         struct ipoctal *ipoctal;
724
725         ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
726         if (ipoctal == NULL)
727                 return -ENOMEM;
728
729         ipoctal->dev = dev;
730         res = ipoctal_inst_slot(ipoctal, dev->bus_nr, dev->slot, dev->irq);
731         if (res)
732                 goto out_uninst;
733
734         list_add_tail(&ipoctal->list, &ipoctal_list);
735         return 0;
736
737 out_uninst:
738         kfree(ipoctal);
739         return res;
740 }
741
742 static void __ipoctal_remove(struct ipoctal *ipoctal)
743 {
744         int i;
745
746         ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
747
748         for (i = 0; i < NR_CHANNELS; i++) {
749                 struct ipoctal_channel *channel = &ipoctal->channel[i];
750                 tty_unregister_device(ipoctal->tty_drv, i);
751                 tty_port_free_xmit_buf(&channel->tty_port);
752         }
753
754         tty_unregister_driver(ipoctal->tty_drv);
755         put_tty_driver(ipoctal->tty_drv);
756         list_del(&ipoctal->list);
757         kfree(ipoctal);
758 }
759
760 static void ipoctal_remove(struct ipack_device *device)
761 {
762         struct ipoctal *ipoctal, *next;
763
764         list_for_each_entry_safe(ipoctal, next, &ipoctal_list, list) {
765                 if (ipoctal->dev == device)
766                         __ipoctal_remove(ipoctal);
767         }
768 }
769
770 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
771         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
772                         IPACK1_DEVICE_ID_SBS_OCTAL_232) },
773         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
774                         IPACK1_DEVICE_ID_SBS_OCTAL_422) },
775         { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
776                         IPACK1_DEVICE_ID_SBS_OCTAL_485) },
777         { 0, },
778 };
779
780 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
781
782 static const struct ipack_driver_ops ipoctal_drv_ops = {
783         .probe  = ipoctal_probe,
784         .remove = ipoctal_remove,
785 };
786
787 static struct ipack_driver driver = {
788         .ops      = &ipoctal_drv_ops,
789         .id_table = ipoctal_ids,
790 };
791
792 static int __init ipoctal_init(void)
793 {
794         return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
795 }
796
797 static void __exit ipoctal_exit(void)
798 {
799         ipack_driver_unregister(&driver);
800 }
801
802 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
803 MODULE_LICENSE("GPL");
804
805 module_init(ipoctal_init);
806 module_exit(ipoctal_exit);