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