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