4 * driver for the GE IP-OCTAL boards
5 * Copyright (c) 2009 Nicolas Serafini, EIC2 SA
6 * Copyright (c) 2010,2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
7 * Copyright (c) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; version 2 of the License.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/device.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/tty.h>
24 #include <linux/serial.h>
25 #include <linux/tty_flip.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/atomic.h>
33 #define IP_OCTAL_MANUFACTURER_ID 0xF0
34 #define IP_OCTAL_232_ID 0x22
35 #define IP_OCTAL_422_ID 0x2A
36 #define IP_OCTAL_485_ID 0x48
38 #define IP_OCTAL_ID_SPACE_VECTOR 0x41
39 #define IP_OCTAL_NB_BLOCKS 4
41 static struct ipack_driver driver;
42 static const struct tty_operations ipoctal_fops;
45 struct list_head list;
46 struct ipack_device *dev;
47 unsigned int board_id;
48 struct scc2698_channel *chan_regs;
49 struct scc2698_block *block_regs;
50 struct ipoctal_stats chan_stats[NR_CHANNELS];
51 char *buffer[NR_CHANNELS];
52 unsigned int nb_bytes[NR_CHANNELS];
53 unsigned int count_wr[NR_CHANNELS];
54 struct ipoctal_config chan_config[NR_CHANNELS];
55 wait_queue_head_t queue[NR_CHANNELS];
56 unsigned short error_flag[NR_CHANNELS];
57 spinlock_t lock[NR_CHANNELS];
58 unsigned int pointer_read[NR_CHANNELS];
59 unsigned int pointer_write[NR_CHANNELS];
60 atomic_t open[NR_CHANNELS];
62 struct tty_port tty_port[NR_CHANNELS];
63 struct tty_driver *tty_drv;
66 /* Linked list to save the registered devices */
67 static LIST_HEAD(ipoctal_list);
69 static inline void ipoctal_write_io_reg(struct ipoctal *ipoctal,
75 offset = ((void __iomem *) dest) - ipoctal->dev->io_space.address;
76 ipoctal->dev->bus->ops->write8(ipoctal->dev, IPACK_IO_SPACE, offset,
80 static inline void ipoctal_write_cr_cmd(struct ipoctal *ipoctal,
84 ipoctal_write_io_reg(ipoctal, dest, value);
87 static inline unsigned char ipoctal_read_io_reg(struct ipoctal *ipoctal,
93 offset = ((void __iomem *) src) - ipoctal->dev->io_space.address;
94 ipoctal->dev->bus->ops->read8(ipoctal->dev, IPACK_IO_SPACE, offset,
99 static struct ipoctal *ipoctal_find_board(struct tty_struct *tty)
103 list_for_each_entry(p, &ipoctal_list, list) {
104 if (tty->driver->major == p->tty_drv->major)
111 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
113 struct ipoctal *ipoctal;
114 int channel = tty->index;
116 ipoctal = ipoctal_find_board(tty);
118 if (ipoctal == NULL) {
119 pr_err("Device not found. Major %d\n", tty->driver->major);
123 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
125 tty->driver_data = ipoctal;
130 static int ipoctal_open(struct tty_struct *tty, struct file *file)
132 int channel = tty->index;
134 struct ipoctal *ipoctal;
136 ipoctal = ipoctal_find_board(tty);
138 if (ipoctal == NULL) {
139 pr_err("Device not found. Major %d\n", tty->driver->major);
143 if (atomic_read(&ipoctal->open[channel]))
146 res = tty_port_open(&ipoctal->tty_port[channel], tty, file);
150 atomic_inc(&ipoctal->open[channel]);
154 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
158 stats->rcv_break = 0;
159 stats->framing_err = 0;
160 stats->overrun_err = 0;
161 stats->parity_err = 0;
164 static void ipoctal_free_channel(struct tty_struct *tty)
166 int channel = tty->index;
167 struct ipoctal *ipoctal = tty->driver_data;
172 ipoctal_reset_stats(&ipoctal->chan_stats[channel]);
173 ipoctal->pointer_read[channel] = 0;
174 ipoctal->pointer_write[channel] = 0;
175 ipoctal->nb_bytes[channel] = 0;
178 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
180 int channel = tty->index;
181 struct ipoctal *ipoctal = tty->driver_data;
183 tty_port_close(&ipoctal->tty_port[channel], tty, filp);
185 if (atomic_dec_and_test(&ipoctal->open[channel]))
186 ipoctal_free_channel(tty);
189 static int ipoctal_get_icount(struct tty_struct *tty,
190 struct serial_icounter_struct *icount)
192 struct ipoctal *ipoctal = tty->driver_data;
193 int channel = tty->index;
199 icount->rx = ipoctal->chan_stats[channel].rx;
200 icount->tx = ipoctal->chan_stats[channel].tx;
201 icount->frame = ipoctal->chan_stats[channel].framing_err;
202 icount->parity = ipoctal->chan_stats[channel].parity_err;
203 icount->brk = ipoctal->chan_stats[channel].rcv_break;
207 static int ipoctal_irq_handler(void *arg)
209 unsigned int channel;
213 unsigned char isr_tx_rdy, isr_rx_rdy;
216 struct tty_struct *tty;
217 struct ipoctal *ipoctal = (struct ipoctal *) arg;
219 /* Check all channels */
220 for (channel = 0; channel < NR_CHANNELS; channel++) {
221 /* If there is no client, skip the check */
222 if (!atomic_read(&ipoctal->open[channel]))
225 tty = tty_port_tty_get(&ipoctal->tty_port[channel]);
230 * The HW is organized in pair of channels.
231 * See which register we need to read from
234 isr = ipoctal_read_io_reg(ipoctal,
235 &ipoctal->block_regs[block].u.r.isr);
236 sr = ipoctal_read_io_reg(ipoctal,
237 &ipoctal->chan_regs[channel].u.r.sr);
239 if ((channel % 2) == 1) {
240 isr_tx_rdy = isr & ISR_TxRDY_B;
241 isr_rx_rdy = isr & ISR_RxRDY_FFULL_B;
243 isr_tx_rdy = isr & ISR_TxRDY_A;
244 isr_rx_rdy = isr & ISR_RxRDY_FFULL_A;
247 /* In case of RS-485, change from TX to RX when finishing TX.
250 if ((ipoctal->board_id == IP_OCTAL_485_ID) &&
251 (sr & SR_TX_EMPTY) &&
252 (ipoctal->nb_bytes[channel] == 0)) {
253 ipoctal_write_io_reg(ipoctal,
254 &ipoctal->chan_regs[channel].u.w.cr,
256 ipoctal_write_cr_cmd(ipoctal,
257 &ipoctal->chan_regs[channel].u.w.cr,
259 ipoctal_write_io_reg(ipoctal,
260 &ipoctal->chan_regs[channel].u.w.cr,
263 wake_up_interruptible(&ipoctal->queue[channel]);
267 if (isr_rx_rdy && (sr & SR_RX_READY)) {
268 value = ipoctal_read_io_reg(ipoctal,
269 &ipoctal->chan_regs[channel].u.r.rhr);
272 /* Error: count statistics */
274 ipoctal_write_cr_cmd(ipoctal,
275 &ipoctal->chan_regs[channel].u.w.cr,
276 CR_CMD_RESET_ERR_STATUS);
278 if (sr & SR_OVERRUN_ERROR) {
279 ipoctal->error_flag[channel] |= UART_OVERRUN;
280 ipoctal->chan_stats[channel].overrun_err++;
281 /* Overrun doesn't affect the current character*/
282 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
284 if (sr & SR_PARITY_ERROR) {
285 ipoctal->error_flag[channel] |= UART_PARITY;
286 ipoctal->chan_stats[channel].parity_err++;
289 if (sr & SR_FRAMING_ERROR) {
290 ipoctal->error_flag[channel] |= UART_FRAMING;
291 ipoctal->chan_stats[channel].framing_err++;
294 if (sr & SR_RECEIVED_BREAK) {
295 ipoctal->error_flag[channel] |= UART_BREAK;
296 ipoctal->chan_stats[channel].rcv_break++;
301 tty_insert_flip_char(tty, value, flag);
304 /* TX of each character */
305 if (isr_tx_rdy && (sr & SR_TX_READY)) {
306 unsigned int *pointer_write =
307 &ipoctal->pointer_write[channel];
309 if (ipoctal->nb_bytes[channel] <= 0) {
310 ipoctal->nb_bytes[channel] = 0;
313 spin_lock(&ipoctal->lock[channel]);
314 value = ipoctal->buffer[channel][*pointer_write];
315 ipoctal_write_io_reg(ipoctal,
316 &ipoctal->chan_regs[channel].u.w.thr,
318 ipoctal->chan_stats[channel].tx++;
319 ipoctal->count_wr[channel]++;
321 *pointer_write = *pointer_write % PAGE_SIZE;
322 ipoctal->nb_bytes[channel]--;
323 spin_unlock(&ipoctal->lock[channel]);
325 if ((ipoctal->nb_bytes[channel] == 0) &&
326 (waitqueue_active(&ipoctal->queue[channel]))) {
328 if (ipoctal->board_id != IP_OCTAL_485_ID) {
330 wake_up_interruptible(&ipoctal->queue[channel]);
335 tty_flip_buffer_push(tty);
341 static int ipoctal_check_model(struct ipack_device *dev, unsigned char *id)
343 unsigned char manufacturerID;
344 unsigned char board_id;
346 dev->bus->ops->read8(dev, IPACK_ID_SPACE,
347 IPACK_IDPROM_OFFSET_MANUFACTURER_ID, &manufacturerID);
348 if (manufacturerID != IP_OCTAL_MANUFACTURER_ID)
351 dev->bus->ops->read8(dev, IPACK_ID_SPACE,
352 IPACK_IDPROM_OFFSET_MODEL, (unsigned char *)&board_id);
355 case IP_OCTAL_232_ID:
356 case IP_OCTAL_422_ID:
357 case IP_OCTAL_485_ID:
367 static const struct tty_port_operations ipoctal_tty_port_ops = {
369 .activate = ipoctal_port_activate,
372 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
373 unsigned int slot, unsigned int vector)
377 struct tty_driver *tty;
379 unsigned char board_id;
381 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev, 0,
384 pr_err("Unable to map slot [%d:%d] ID space!\n", bus_nr, slot);
388 res = ipoctal_check_model(ipoctal->dev, &board_id);
390 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev,
392 goto out_unregister_id_space;
394 ipoctal->board_id = board_id;
396 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev, 0,
399 pr_err("Unable to map slot [%d:%d] IO space!\n", bus_nr, slot);
400 goto out_unregister_id_space;
403 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev,
404 0x8000, IPACK_MEM_SPACE);
406 pr_err("Unable to map slot [%d:%d] MEM space!\n", bus_nr, slot);
407 goto out_unregister_io_space;
410 /* Save the virtual address to access the registers easily */
412 (struct scc2698_channel *) ipoctal->dev->io_space.address;
413 ipoctal->block_regs =
414 (struct scc2698_block *) ipoctal->dev->io_space.address;
416 /* Disable RX and TX before touching anything */
417 for (i = 0; i < NR_CHANNELS ; i++) {
418 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
419 CR_DISABLE_RX | CR_DISABLE_TX);
422 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
423 ipoctal_write_io_reg(ipoctal,
424 &ipoctal->block_regs[i].u.w.acr,
426 ipoctal_write_io_reg(ipoctal,
427 &ipoctal->block_regs[i].u.w.opcr,
428 OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN |
430 ipoctal_write_io_reg(ipoctal,
431 &ipoctal->block_regs[i].u.w.imr,
432 IMR_TxRDY_A | IMR_RxRDY_FFULL_A |
433 IMR_DELTA_BREAK_A | IMR_TxRDY_B |
434 IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B);
438 * IP-OCTAL has different addresses to copy its IRQ vector.
439 * Depending of the carrier these addresses are accesible or not.
440 * More info in the datasheet.
442 ipoctal->dev->bus->ops->request_irq(ipoctal->dev, vector,
443 ipoctal_irq_handler, ipoctal);
444 ipoctal->dev->bus->ops->write8(ipoctal->dev, IPACK_ID_SPACE, 0, vector);
446 /* Register the TTY device */
448 /* Each IP-OCTAL channel is a TTY port */
449 tty = alloc_tty_driver(NR_CHANNELS);
453 goto out_unregister_slot_unmap;
456 /* Fill struct tty_driver with ipoctal data */
457 tty->owner = THIS_MODULE;
458 tty->driver_name = "ipoctal";
459 sprintf(name, "ipoctal.%d.%d.", bus_nr, slot);
463 tty->minor_start = 0;
464 tty->type = TTY_DRIVER_TYPE_SERIAL;
465 tty->subtype = SERIAL_TYPE_NORMAL;
466 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
467 tty->init_termios = tty_std_termios;
468 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
469 tty->init_termios.c_ispeed = 9600;
470 tty->init_termios.c_ospeed = 9600;
472 tty_set_operations(tty, &ipoctal_fops);
473 res = tty_register_driver(tty);
475 pr_err("Can't register tty driver.\n");
477 goto out_unregister_slot_unmap;
480 /* Save struct tty_driver for use it when uninstalling the device */
481 ipoctal->tty_drv = tty;
483 for (i = 0; i < NR_CHANNELS; i++) {
484 tty_port_init(&ipoctal->tty_port[i]);
485 tty_port_alloc_xmit_buf(&ipoctal->tty_port[i]);
486 ipoctal->tty_port[i].ops = &ipoctal_tty_port_ops;
488 ipoctal_reset_stats(&ipoctal->chan_stats[i]);
489 ipoctal->nb_bytes[i] = 0;
490 init_waitqueue_head(&ipoctal->queue[i]);
491 ipoctal->error_flag[i] = UART_NOERROR;
493 spin_lock_init(&ipoctal->lock[i]);
494 ipoctal->pointer_read[i] = 0;
495 ipoctal->pointer_write[i] = 0;
496 ipoctal->nb_bytes[i] = 0;
497 tty_register_device(tty, i, NULL);
500 * Enable again the RX. TX will be enabled when
501 * there is something to send
503 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
509 out_unregister_slot_unmap:
510 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_ID_SPACE);
511 out_unregister_io_space:
512 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_IO_SPACE);
513 out_unregister_id_space:
514 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_MEM_SPACE);
518 static inline int ipoctal_copy_write_buffer(struct ipoctal *ipoctal,
519 unsigned int channel,
520 const unsigned char *buf,
525 unsigned int *pointer_read = &ipoctal->pointer_read[channel];
527 /* Copy the bytes from the user buffer to the internal one */
528 for (i = 0; i < count; i++) {
529 if (i <= (PAGE_SIZE - ipoctal->nb_bytes[channel])) {
530 spin_lock_irqsave(&ipoctal->lock[channel], flags);
531 ipoctal->tty_port[channel].xmit_buf[*pointer_read] = buf[i];
532 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
533 ipoctal->nb_bytes[channel]++;
534 spin_unlock_irqrestore(&ipoctal->lock[channel], flags);
542 static int ipoctal_write(struct ipoctal *ipoctal, unsigned int channel,
543 const unsigned char *buf, int count)
545 ipoctal->nb_bytes[channel] = 0;
546 ipoctal->count_wr[channel] = 0;
548 ipoctal_copy_write_buffer(ipoctal, channel, buf, count);
550 ipoctal->error_flag[channel] = UART_NOERROR;
552 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
553 if (ipoctal->board_id == IP_OCTAL_485_ID) {
554 ipoctal_write_io_reg(ipoctal,
555 &ipoctal->chan_regs[channel].u.w.cr,
557 ipoctal_write_cr_cmd(ipoctal,
558 &ipoctal->chan_regs[channel].u.w.cr,
563 * Send a packet and then disable TX to avoid failure after several send
566 ipoctal_write_io_reg(ipoctal,
567 &ipoctal->chan_regs[channel].u.w.cr,
569 wait_event_interruptible(ipoctal->queue[channel], ipoctal->write);
570 ipoctal_write_io_reg(ipoctal,
571 &ipoctal->chan_regs[channel].u.w.cr,
575 return ipoctal->count_wr[channel];
578 static int ipoctal_write_tty(struct tty_struct *tty,
579 const unsigned char *buf, int count)
581 unsigned int channel = tty->index;
582 struct ipoctal *ipoctal = tty->driver_data;
584 return ipoctal_write(ipoctal, channel, buf, count);
587 static int ipoctal_write_room(struct tty_struct *tty)
589 int channel = tty->index;
590 struct ipoctal *ipoctal = tty->driver_data;
592 return PAGE_SIZE - ipoctal->nb_bytes[channel];
595 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
597 int channel = tty->index;
598 struct ipoctal *ipoctal = tty->driver_data;
600 return ipoctal->nb_bytes[channel];
603 static void ipoctal_set_termios(struct tty_struct *tty,
604 struct ktermios *old_termios)
607 unsigned char mr1 = 0;
608 unsigned char mr2 = 0;
609 unsigned char csr = 0;
610 unsigned int channel = tty->index;
611 struct ipoctal *ipoctal = tty->driver_data;
614 cflag = tty->termios->c_cflag;
616 /* Disable and reset everything before change the setup */
617 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
618 CR_DISABLE_RX | CR_DISABLE_TX);
619 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
621 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
623 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
624 CR_CMD_RESET_ERR_STATUS);
625 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
628 /* Set Bits per chars */
629 switch (cflag & CSIZE) {
631 mr1 |= MR1_CHRL_6_BITS;
634 mr1 |= MR1_CHRL_7_BITS;
638 mr1 |= MR1_CHRL_8_BITS;
639 /* By default, select CS8 */
640 tty->termios->c_cflag = (cflag & ~CSIZE) | CS8;
647 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
649 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
651 mr1 |= MR1_PARITY_OFF;
653 /* Mark or space parity is not supported */
654 tty->termios->c_cflag &= ~CMSPAR;
658 mr2 |= MR2_STOP_BITS_LENGTH_2;
660 mr2 |= MR2_STOP_BITS_LENGTH_1;
662 /* Set the flow control */
663 switch (ipoctal->board_id) {
664 case IP_OCTAL_232_ID:
665 if (cflag & CRTSCTS) {
666 mr1 |= MR1_RxRTS_CONTROL_ON;
667 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
668 ipoctal->chan_config[channel].flow_control = 1;
670 mr1 |= MR1_RxRTS_CONTROL_OFF;
671 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
672 ipoctal->chan_config[channel].flow_control = 0;
675 case IP_OCTAL_422_ID:
676 mr1 |= MR1_RxRTS_CONTROL_OFF;
677 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
678 ipoctal->chan_config[channel].flow_control = 0;
680 case IP_OCTAL_485_ID:
681 mr1 |= MR1_RxRTS_CONTROL_OFF;
682 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
683 ipoctal->chan_config[channel].flow_control = 0;
690 baud = tty_get_baud_rate(tty);
691 tty_termios_encode_baud_rate(tty->termios, baud, baud);
694 switch (tty->termios->c_ospeed) {
696 csr |= TX_CLK_75 | RX_CLK_75;
699 csr |= TX_CLK_110 | RX_CLK_110;
702 csr |= TX_CLK_150 | RX_CLK_150;
705 csr |= TX_CLK_300 | RX_CLK_300;
708 csr |= TX_CLK_600 | RX_CLK_600;
711 csr |= TX_CLK_1200 | RX_CLK_1200;
714 csr |= TX_CLK_1800 | RX_CLK_1800;
717 csr |= TX_CLK_2000 | RX_CLK_2000;
720 csr |= TX_CLK_2400 | RX_CLK_2400;
723 csr |= TX_CLK_4800 | RX_CLK_4800;
726 csr |= TX_CLK_9600 | RX_CLK_9600;
729 csr |= TX_CLK_19200 | RX_CLK_19200;
733 csr |= TX_CLK_38400 | RX_CLK_38400;
734 /* In case of default, we establish 38400 bps */
735 tty_termios_encode_baud_rate(tty->termios, 38400, 38400);
739 mr1 |= MR1_ERROR_CHAR;
740 mr1 |= MR1_RxINT_RxRDY;
742 /* Write the control registers */
743 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.mr, mr1);
744 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.mr, mr2);
745 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.csr, csr);
747 /* save the setup in the structure */
748 ipoctal->chan_config[channel].baud = tty_get_baud_rate(tty);
749 ipoctal->chan_config[channel].bits_per_char = cflag & CSIZE;
750 ipoctal->chan_config[channel].parity = cflag & PARENB;
751 ipoctal->chan_config[channel].stop_bits = cflag & CSTOPB;
753 /* Enable again the RX */
754 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
758 static void ipoctal_hangup(struct tty_struct *tty)
761 int channel = tty->index;
762 struct ipoctal *ipoctal = tty->driver_data;
767 spin_lock_irqsave(&ipoctal->lock[channel], flags);
768 ipoctal->nb_bytes[channel] = 0;
769 ipoctal->pointer_read[channel] = 0;
770 ipoctal->pointer_write[channel] = 0;
771 spin_unlock_irqrestore(&ipoctal->lock[channel], flags);
773 tty_port_hangup(&ipoctal->tty_port[channel]);
775 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
776 CR_DISABLE_RX | CR_DISABLE_TX);
777 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
779 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
781 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
782 CR_CMD_RESET_ERR_STATUS);
783 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
786 clear_bit(ASYNCB_INITIALIZED, &ipoctal->tty_port[channel].flags);
787 wake_up_interruptible(&ipoctal->tty_port[channel].open_wait);
790 static const struct tty_operations ipoctal_fops = {
792 .open = ipoctal_open,
793 .close = ipoctal_close,
794 .write = ipoctal_write_tty,
795 .set_termios = ipoctal_set_termios,
796 .write_room = ipoctal_write_room,
797 .chars_in_buffer = ipoctal_chars_in_buffer,
798 .get_icount = ipoctal_get_icount,
799 .hangup = ipoctal_hangup,
802 static int ipoctal_match(struct ipack_device *dev)
805 unsigned char board_id;
807 if ((!dev->bus->ops) || (!dev->bus->ops->map_space) ||
808 (!dev->bus->ops->unmap_space))
811 res = dev->bus->ops->map_space(dev, 0, IPACK_ID_SPACE);
815 res = ipoctal_check_model(dev, &board_id);
816 dev->bus->ops->unmap_space(dev, IPACK_ID_SPACE);
823 static int ipoctal_probe(struct ipack_device *dev)
826 struct ipoctal *ipoctal;
828 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
833 res = ipoctal_inst_slot(ipoctal, dev->bus_nr, dev->slot, dev->irq);
837 list_add_tail(&ipoctal->list, &ipoctal_list);
845 static void __ipoctal_remove(struct ipoctal *ipoctal)
849 for (i = 0; i < NR_CHANNELS; i++) {
850 tty_unregister_device(ipoctal->tty_drv, i);
851 tty_port_free_xmit_buf(&ipoctal->tty_port[i]);
854 tty_unregister_driver(ipoctal->tty_drv);
855 put_tty_driver(ipoctal->tty_drv);
857 /* Tell the carrier board to free all the resources for this device */
858 if (ipoctal->dev->bus->ops->remove_device != NULL)
859 ipoctal->dev->bus->ops->remove_device(ipoctal->dev);
861 list_del(&ipoctal->list);
865 static void ipoctal_remove(struct ipack_device *device)
867 struct ipoctal *ipoctal, *next;
869 list_for_each_entry_safe(ipoctal, next, &ipoctal_list, list) {
870 if (ipoctal->dev == device)
871 __ipoctal_remove(ipoctal);
875 static struct ipack_driver_ops ipoctal_drv_ops = {
876 .match = ipoctal_match,
877 .probe = ipoctal_probe,
878 .remove = ipoctal_remove,
881 static int __init ipoctal_init(void)
883 driver.ops = &ipoctal_drv_ops;
884 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
887 static void __exit ipoctal_exit(void)
889 struct ipoctal *p, *next;
891 list_for_each_entry_safe(p, next, &ipoctal_list, list)
894 ipack_driver_unregister(&driver);
897 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
898 MODULE_LICENSE("GPL");
900 module_init(ipoctal_init);
901 module_exit(ipoctal_exit);