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