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