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