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