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