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 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/sched.h>
18 #include <linux/tty.h>
19 #include <linux/serial.h>
20 #include <linux/tty_flip.h>
21 #include <linux/slab.h>
22 #include <linux/atomic.h>
27 #define IP_OCTAL_MANUFACTURER_ID 0xF0
28 #define IP_OCTAL_232_ID 0x22
29 #define IP_OCTAL_422_ID 0x2A
30 #define IP_OCTAL_485_ID 0x48
32 #define IP_OCTAL_ID_SPACE_VECTOR 0x41
33 #define IP_OCTAL_NB_BLOCKS 4
35 static struct ipack_driver driver;
36 static const struct tty_operations ipoctal_fops;
39 struct list_head list;
40 struct ipack_device *dev;
41 unsigned int board_id;
42 struct scc2698_channel *chan_regs;
43 struct scc2698_block *block_regs;
44 struct ipoctal_stats chan_stats[NR_CHANNELS];
45 unsigned int nb_bytes[NR_CHANNELS];
46 unsigned int count_wr[NR_CHANNELS];
47 wait_queue_head_t queue[NR_CHANNELS];
48 spinlock_t lock[NR_CHANNELS];
49 unsigned int pointer_read[NR_CHANNELS];
50 unsigned int pointer_write[NR_CHANNELS];
51 atomic_t open[NR_CHANNELS];
53 struct tty_port tty_port[NR_CHANNELS];
54 struct tty_driver *tty_drv;
57 /* Linked list to save the registered devices */
58 static LIST_HEAD(ipoctal_list);
60 static inline void ipoctal_write_io_reg(struct ipoctal *ipoctal,
66 offset = ((void __iomem *) dest) - ipoctal->dev->io_space.address;
67 ipoctal->dev->bus->ops->write8(ipoctal->dev, IPACK_IO_SPACE, offset,
71 static inline void ipoctal_write_cr_cmd(struct ipoctal *ipoctal,
75 ipoctal_write_io_reg(ipoctal, dest, value);
78 static inline unsigned char ipoctal_read_io_reg(struct ipoctal *ipoctal,
84 offset = ((void __iomem *) src) - ipoctal->dev->io_space.address;
85 ipoctal->dev->bus->ops->read8(ipoctal->dev, IPACK_IO_SPACE, offset,
90 static struct ipoctal *ipoctal_find_board(struct tty_struct *tty)
94 list_for_each_entry(p, &ipoctal_list, list) {
95 if (tty->driver->major == p->tty_drv->major)
102 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
104 struct ipoctal *ipoctal;
105 int channel = tty->index;
107 ipoctal = ipoctal_find_board(tty);
109 if (ipoctal == NULL) {
110 dev_err(tty->dev, "Device not found. Major %d\n",
115 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
120 static int ipoctal_open(struct tty_struct *tty, struct file *file)
122 int channel = tty->index;
124 struct ipoctal *ipoctal;
126 ipoctal = ipoctal_find_board(tty);
128 if (ipoctal == NULL) {
129 dev_err(tty->dev, "Device not found. Major %d\n",
134 if (atomic_read(&ipoctal->open[channel]))
137 tty->driver_data = ipoctal;
139 res = tty_port_open(&ipoctal->tty_port[channel], tty, file);
143 atomic_inc(&ipoctal->open[channel]);
147 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
151 stats->rcv_break = 0;
152 stats->framing_err = 0;
153 stats->overrun_err = 0;
154 stats->parity_err = 0;
157 static void ipoctal_free_channel(struct tty_struct *tty)
159 int channel = tty->index;
160 struct ipoctal *ipoctal = tty->driver_data;
165 ipoctal_reset_stats(&ipoctal->chan_stats[channel]);
166 ipoctal->pointer_read[channel] = 0;
167 ipoctal->pointer_write[channel] = 0;
168 ipoctal->nb_bytes[channel] = 0;
171 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
173 int channel = tty->index;
174 struct ipoctal *ipoctal = tty->driver_data;
176 tty_port_close(&ipoctal->tty_port[channel], tty, filp);
178 if (atomic_dec_and_test(&ipoctal->open[channel]))
179 ipoctal_free_channel(tty);
182 static int ipoctal_get_icount(struct tty_struct *tty,
183 struct serial_icounter_struct *icount)
185 struct ipoctal *ipoctal = tty->driver_data;
186 int channel = tty->index;
192 icount->rx = ipoctal->chan_stats[channel].rx;
193 icount->tx = ipoctal->chan_stats[channel].tx;
194 icount->frame = ipoctal->chan_stats[channel].framing_err;
195 icount->parity = ipoctal->chan_stats[channel].parity_err;
196 icount->brk = ipoctal->chan_stats[channel].rcv_break;
200 static int ipoctal_irq_handler(void *arg)
202 unsigned int channel;
206 unsigned char isr_tx_rdy, isr_rx_rdy;
209 struct tty_struct *tty;
210 struct ipoctal *ipoctal = (struct ipoctal *) arg;
212 /* Check all channels */
213 for (channel = 0; channel < NR_CHANNELS; channel++) {
214 /* If there is no client, skip the check */
215 if (!atomic_read(&ipoctal->open[channel]))
218 tty = tty_port_tty_get(&ipoctal->tty_port[channel]);
223 * The HW is organized in pair of channels.
224 * See which register we need to read from
227 isr = ipoctal_read_io_reg(ipoctal,
228 &ipoctal->block_regs[block].u.r.isr);
229 sr = ipoctal_read_io_reg(ipoctal,
230 &ipoctal->chan_regs[channel].u.r.sr);
232 if ((channel % 2) == 1) {
233 isr_tx_rdy = isr & ISR_TxRDY_B;
234 isr_rx_rdy = isr & ISR_RxRDY_FFULL_B;
236 isr_tx_rdy = isr & ISR_TxRDY_A;
237 isr_rx_rdy = isr & ISR_RxRDY_FFULL_A;
240 /* In case of RS-485, change from TX to RX when finishing TX.
243 if ((ipoctal->board_id == IP_OCTAL_485_ID) &&
244 (sr & SR_TX_EMPTY) &&
245 (ipoctal->nb_bytes[channel] == 0)) {
246 ipoctal_write_io_reg(ipoctal,
247 &ipoctal->chan_regs[channel].u.w.cr,
249 ipoctal_write_cr_cmd(ipoctal,
250 &ipoctal->chan_regs[channel].u.w.cr,
252 ipoctal_write_io_reg(ipoctal,
253 &ipoctal->chan_regs[channel].u.w.cr,
256 wake_up_interruptible(&ipoctal->queue[channel]);
260 if (isr_rx_rdy && (sr & SR_RX_READY)) {
261 value = ipoctal_read_io_reg(ipoctal,
262 &ipoctal->chan_regs[channel].u.r.rhr);
265 /* Error: count statistics */
267 ipoctal_write_cr_cmd(ipoctal,
268 &ipoctal->chan_regs[channel].u.w.cr,
269 CR_CMD_RESET_ERR_STATUS);
271 if (sr & SR_OVERRUN_ERROR) {
272 ipoctal->chan_stats[channel].overrun_err++;
273 /* Overrun doesn't affect the current character*/
274 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
276 if (sr & SR_PARITY_ERROR) {
277 ipoctal->chan_stats[channel].parity_err++;
280 if (sr & SR_FRAMING_ERROR) {
281 ipoctal->chan_stats[channel].framing_err++;
284 if (sr & SR_RECEIVED_BREAK) {
285 ipoctal->chan_stats[channel].rcv_break++;
290 tty_insert_flip_char(tty, value, flag);
293 /* TX of each character */
294 if (isr_tx_rdy && (sr & SR_TX_READY)) {
295 unsigned int *pointer_write =
296 &ipoctal->pointer_write[channel];
298 if (ipoctal->nb_bytes[channel] <= 0) {
299 ipoctal->nb_bytes[channel] = 0;
303 value = ipoctal->tty_port[channel].xmit_buf[*pointer_write];
304 ipoctal_write_io_reg(ipoctal,
305 &ipoctal->chan_regs[channel].u.w.thr,
307 ipoctal->chan_stats[channel].tx++;
308 ipoctal->count_wr[channel]++;
310 *pointer_write = *pointer_write % PAGE_SIZE;
311 ipoctal->nb_bytes[channel]--;
313 if ((ipoctal->nb_bytes[channel] == 0) &&
314 (waitqueue_active(&ipoctal->queue[channel]))) {
316 if (ipoctal->board_id != IP_OCTAL_485_ID) {
318 wake_up_interruptible(&ipoctal->queue[channel]);
323 tty_flip_buffer_push(tty);
329 static int ipoctal_check_model(struct ipack_device *dev, unsigned char *id)
331 unsigned char manufacturerID;
332 unsigned char board_id;
334 dev->bus->ops->read8(dev, IPACK_ID_SPACE,
335 IPACK_IDPROM_OFFSET_MANUFACTURER_ID, &manufacturerID);
336 if (manufacturerID != IP_OCTAL_MANUFACTURER_ID)
339 dev->bus->ops->read8(dev, IPACK_ID_SPACE,
340 IPACK_IDPROM_OFFSET_MODEL, (unsigned char *)&board_id);
343 case IP_OCTAL_232_ID:
344 case IP_OCTAL_422_ID:
345 case IP_OCTAL_485_ID:
355 static const struct tty_port_operations ipoctal_tty_port_ops = {
357 .activate = ipoctal_port_activate,
360 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
361 unsigned int slot, unsigned int vector)
365 struct tty_driver *tty;
367 unsigned char board_id;
369 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev, 0,
372 dev_err(&ipoctal->dev->dev,
373 "Unable to map slot [%d:%d] ID space!\n",
378 res = ipoctal_check_model(ipoctal->dev, &board_id);
380 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev,
382 goto out_unregister_id_space;
384 ipoctal->board_id = board_id;
386 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev, 0,
389 dev_err(&ipoctal->dev->dev,
390 "Unable to map slot [%d:%d] IO space!\n",
392 goto out_unregister_id_space;
395 res = ipoctal->dev->bus->ops->map_space(ipoctal->dev,
396 0x8000, IPACK_MEM_SPACE);
398 dev_err(&ipoctal->dev->dev,
399 "Unable to map slot [%d:%d] MEM space!\n",
401 goto out_unregister_io_space;
404 /* Save the virtual address to access the registers easily */
406 (struct scc2698_channel *) ipoctal->dev->io_space.address;
407 ipoctal->block_regs =
408 (struct scc2698_block *) ipoctal->dev->io_space.address;
410 /* Disable RX and TX before touching anything */
411 for (i = 0; i < NR_CHANNELS ; i++) {
412 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
413 CR_DISABLE_RX | CR_DISABLE_TX);
414 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
416 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
418 ipoctal_write_io_reg(ipoctal,
419 &ipoctal->chan_regs[i].u.w.mr,
420 MR1_CHRL_8_BITS | MR1_ERROR_CHAR |
421 MR1_RxINT_RxRDY); /* mr1 */
422 ipoctal_write_io_reg(ipoctal,
423 &ipoctal->chan_regs[i].u.w.mr,
425 ipoctal_write_io_reg(ipoctal,
426 &ipoctal->chan_regs[i].u.w.csr,
427 TX_CLK_9600 | RX_CLK_9600);
430 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
431 ipoctal_write_io_reg(ipoctal,
432 &ipoctal->block_regs[i].u.w.acr,
434 ipoctal_write_io_reg(ipoctal,
435 &ipoctal->block_regs[i].u.w.opcr,
436 OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN |
438 ipoctal_write_io_reg(ipoctal,
439 &ipoctal->block_regs[i].u.w.imr,
440 IMR_TxRDY_A | IMR_RxRDY_FFULL_A |
441 IMR_DELTA_BREAK_A | IMR_TxRDY_B |
442 IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B);
446 * IP-OCTAL has different addresses to copy its IRQ vector.
447 * Depending of the carrier these addresses are accesible or not.
448 * More info in the datasheet.
450 ipoctal->dev->bus->ops->request_irq(ipoctal->dev, vector,
451 ipoctal_irq_handler, ipoctal);
452 ipoctal->dev->bus->ops->write8(ipoctal->dev, IPACK_MEM_SPACE, 0,
455 /* Register the TTY device */
457 /* Each IP-OCTAL channel is a TTY port */
458 tty = alloc_tty_driver(NR_CHANNELS);
462 goto out_unregister_slot_unmap;
465 /* Fill struct tty_driver with ipoctal data */
466 tty->owner = THIS_MODULE;
467 tty->driver_name = "ipoctal";
468 sprintf(name, "ipoctal.%d.%d.", bus_nr, slot);
472 tty->minor_start = 0;
473 tty->type = TTY_DRIVER_TYPE_SERIAL;
474 tty->subtype = SERIAL_TYPE_NORMAL;
475 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
476 tty->init_termios = tty_std_termios;
477 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
478 tty->init_termios.c_ispeed = 9600;
479 tty->init_termios.c_ospeed = 9600;
481 tty_set_operations(tty, &ipoctal_fops);
482 res = tty_register_driver(tty);
484 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
486 goto out_unregister_slot_unmap;
489 /* Save struct tty_driver for use it when uninstalling the device */
490 ipoctal->tty_drv = tty;
492 for (i = 0; i < NR_CHANNELS; i++) {
493 tty_port_init(&ipoctal->tty_port[i]);
494 tty_port_alloc_xmit_buf(&ipoctal->tty_port[i]);
495 ipoctal->tty_port[i].ops = &ipoctal_tty_port_ops;
497 ipoctal_reset_stats(&ipoctal->chan_stats[i]);
498 ipoctal->nb_bytes[i] = 0;
499 init_waitqueue_head(&ipoctal->queue[i]);
501 spin_lock_init(&ipoctal->lock[i]);
502 ipoctal->pointer_read[i] = 0;
503 ipoctal->pointer_write[i] = 0;
504 ipoctal->nb_bytes[i] = 0;
505 tty_register_device(tty, i, NULL);
508 * Enable again the RX. TX will be enabled when
509 * there is something to send
511 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[i].u.w.cr,
517 out_unregister_slot_unmap:
518 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_ID_SPACE);
519 out_unregister_io_space:
520 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_IO_SPACE);
521 out_unregister_id_space:
522 ipoctal->dev->bus->ops->unmap_space(ipoctal->dev, IPACK_MEM_SPACE);
526 static inline int ipoctal_copy_write_buffer(struct ipoctal *ipoctal,
527 unsigned int channel,
528 const unsigned char *buf,
533 unsigned int *pointer_read = &ipoctal->pointer_read[channel];
535 /* Copy the bytes from the user buffer to the internal one */
536 for (i = 0; i < count; i++) {
537 if (i <= (PAGE_SIZE - ipoctal->nb_bytes[channel])) {
538 spin_lock_irqsave(&ipoctal->lock[channel], flags);
539 ipoctal->tty_port[channel].xmit_buf[*pointer_read] = buf[i];
540 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
541 ipoctal->nb_bytes[channel]++;
542 spin_unlock_irqrestore(&ipoctal->lock[channel], flags);
550 static int ipoctal_write(struct ipoctal *ipoctal, unsigned int channel,
551 const unsigned char *buf, int count)
553 ipoctal->nb_bytes[channel] = 0;
554 ipoctal->count_wr[channel] = 0;
556 ipoctal_copy_write_buffer(ipoctal, channel, buf, count);
558 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
559 if (ipoctal->board_id == IP_OCTAL_485_ID) {
560 ipoctal_write_io_reg(ipoctal,
561 &ipoctal->chan_regs[channel].u.w.cr,
563 ipoctal_write_cr_cmd(ipoctal,
564 &ipoctal->chan_regs[channel].u.w.cr,
569 * Send a packet and then disable TX to avoid failure after several send
572 ipoctal_write_io_reg(ipoctal,
573 &ipoctal->chan_regs[channel].u.w.cr,
575 wait_event_interruptible(ipoctal->queue[channel], ipoctal->write);
576 ipoctal_write_io_reg(ipoctal,
577 &ipoctal->chan_regs[channel].u.w.cr,
581 return ipoctal->count_wr[channel];
584 static int ipoctal_write_tty(struct tty_struct *tty,
585 const unsigned char *buf, int count)
587 unsigned int channel = tty->index;
588 struct ipoctal *ipoctal = tty->driver_data;
590 return ipoctal_write(ipoctal, channel, buf, count);
593 static int ipoctal_write_room(struct tty_struct *tty)
595 int channel = tty->index;
596 struct ipoctal *ipoctal = tty->driver_data;
598 return PAGE_SIZE - ipoctal->nb_bytes[channel];
601 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
603 int channel = tty->index;
604 struct ipoctal *ipoctal = tty->driver_data;
606 return ipoctal->nb_bytes[channel];
609 static void ipoctal_set_termios(struct tty_struct *tty,
610 struct ktermios *old_termios)
613 unsigned char mr1 = 0;
614 unsigned char mr2 = 0;
615 unsigned char csr = 0;
616 unsigned int channel = tty->index;
617 struct ipoctal *ipoctal = tty->driver_data;
620 cflag = tty->termios->c_cflag;
622 /* Disable and reset everything before change the setup */
623 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
624 CR_DISABLE_RX | CR_DISABLE_TX);
625 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
627 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
629 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
630 CR_CMD_RESET_ERR_STATUS);
631 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
634 /* Set Bits per chars */
635 switch (cflag & CSIZE) {
637 mr1 |= MR1_CHRL_6_BITS;
640 mr1 |= MR1_CHRL_7_BITS;
644 mr1 |= MR1_CHRL_8_BITS;
645 /* By default, select CS8 */
646 tty->termios->c_cflag = (cflag & ~CSIZE) | CS8;
653 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
655 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
657 mr1 |= MR1_PARITY_OFF;
659 /* Mark or space parity is not supported */
660 tty->termios->c_cflag &= ~CMSPAR;
664 mr2 |= MR2_STOP_BITS_LENGTH_2;
666 mr2 |= MR2_STOP_BITS_LENGTH_1;
668 /* Set the flow control */
669 switch (ipoctal->board_id) {
670 case IP_OCTAL_232_ID:
671 if (cflag & CRTSCTS) {
672 mr1 |= MR1_RxRTS_CONTROL_ON;
673 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
675 mr1 |= MR1_RxRTS_CONTROL_OFF;
676 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
679 case IP_OCTAL_422_ID:
680 mr1 |= MR1_RxRTS_CONTROL_OFF;
681 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
683 case IP_OCTAL_485_ID:
684 mr1 |= MR1_RxRTS_CONTROL_OFF;
685 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
692 baud = tty_get_baud_rate(tty);
693 tty_termios_encode_baud_rate(tty->termios, baud, baud);
696 switch (tty->termios->c_ospeed) {
698 csr |= TX_CLK_75 | RX_CLK_75;
701 csr |= TX_CLK_110 | RX_CLK_110;
704 csr |= TX_CLK_150 | RX_CLK_150;
707 csr |= TX_CLK_300 | RX_CLK_300;
710 csr |= TX_CLK_600 | RX_CLK_600;
713 csr |= TX_CLK_1200 | RX_CLK_1200;
716 csr |= TX_CLK_1800 | RX_CLK_1800;
719 csr |= TX_CLK_2000 | RX_CLK_2000;
722 csr |= TX_CLK_2400 | RX_CLK_2400;
725 csr |= TX_CLK_4800 | RX_CLK_4800;
728 csr |= TX_CLK_9600 | RX_CLK_9600;
731 csr |= TX_CLK_19200 | RX_CLK_19200;
735 csr |= TX_CLK_38400 | RX_CLK_38400;
736 /* In case of default, we establish 38400 bps */
737 tty_termios_encode_baud_rate(tty->termios, 38400, 38400);
741 mr1 |= MR1_ERROR_CHAR;
742 mr1 |= MR1_RxINT_RxRDY;
744 /* Write the control registers */
745 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.mr, mr1);
746 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.mr, mr2);
747 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.csr, csr);
749 /* Enable again the RX */
750 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
754 static void ipoctal_hangup(struct tty_struct *tty)
757 int channel = tty->index;
758 struct ipoctal *ipoctal = tty->driver_data;
763 spin_lock_irqsave(&ipoctal->lock[channel], flags);
764 ipoctal->nb_bytes[channel] = 0;
765 ipoctal->pointer_read[channel] = 0;
766 ipoctal->pointer_write[channel] = 0;
767 spin_unlock_irqrestore(&ipoctal->lock[channel], flags);
769 tty_port_hangup(&ipoctal->tty_port[channel]);
771 ipoctal_write_io_reg(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
772 CR_DISABLE_RX | CR_DISABLE_TX);
773 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
775 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
777 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
778 CR_CMD_RESET_ERR_STATUS);
779 ipoctal_write_cr_cmd(ipoctal, &ipoctal->chan_regs[channel].u.w.cr,
782 clear_bit(ASYNCB_INITIALIZED, &ipoctal->tty_port[channel].flags);
783 wake_up_interruptible(&ipoctal->tty_port[channel].open_wait);
786 static const struct tty_operations ipoctal_fops = {
788 .open = ipoctal_open,
789 .close = ipoctal_close,
790 .write = ipoctal_write_tty,
791 .set_termios = ipoctal_set_termios,
792 .write_room = ipoctal_write_room,
793 .chars_in_buffer = ipoctal_chars_in_buffer,
794 .get_icount = ipoctal_get_icount,
795 .hangup = ipoctal_hangup,
798 static int ipoctal_match(struct ipack_device *dev)
801 unsigned char board_id;
803 if ((!dev->bus->ops) || (!dev->bus->ops->map_space) ||
804 (!dev->bus->ops->unmap_space))
807 res = dev->bus->ops->map_space(dev, 0, IPACK_ID_SPACE);
811 res = ipoctal_check_model(dev, &board_id);
812 dev->bus->ops->unmap_space(dev, IPACK_ID_SPACE);
819 static int ipoctal_probe(struct ipack_device *dev)
822 struct ipoctal *ipoctal;
824 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
829 res = ipoctal_inst_slot(ipoctal, dev->bus_nr, dev->slot, dev->irq);
833 list_add_tail(&ipoctal->list, &ipoctal_list);
841 static void __ipoctal_remove(struct ipoctal *ipoctal)
845 for (i = 0; i < NR_CHANNELS; i++) {
846 tty_unregister_device(ipoctal->tty_drv, i);
847 tty_port_free_xmit_buf(&ipoctal->tty_port[i]);
850 tty_unregister_driver(ipoctal->tty_drv);
851 put_tty_driver(ipoctal->tty_drv);
852 list_del(&ipoctal->list);
856 static void ipoctal_remove(struct ipack_device *device)
858 struct ipoctal *ipoctal, *next;
860 list_for_each_entry_safe(ipoctal, next, &ipoctal_list, list) {
861 if (ipoctal->dev == device)
862 __ipoctal_remove(ipoctal);
866 static struct ipack_driver_ops ipoctal_drv_ops = {
867 .match = ipoctal_match,
868 .probe = ipoctal_probe,
869 .remove = ipoctal_remove,
872 static int __init ipoctal_init(void)
874 driver.ops = &ipoctal_drv_ops;
875 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
878 static void __exit ipoctal_exit(void)
880 struct ipoctal *p, *next;
882 list_for_each_entry_safe(p, next, &ipoctal_list, list)
883 p->dev->bus->ops->remove_device(p->dev);
885 ipack_driver_unregister(&driver);
888 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
889 MODULE_LICENSE("GPL");
891 module_init(ipoctal_init);
892 module_exit(ipoctal_exit);