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