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