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