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