Skip to contentSkip to conversation

Add SUNIX Multi-I/O card device driver

PATCH In review Morris Ku 3 participants 4 messages Mar 8, 2019 – Mar 11, 2019

lore
Share & tools
lore

Morris Ku

author
#0 lore raw

PATCH 2/4 Add SUNIX Multi-I/O card device driver

Driver for SUNIX Multi-I/O card.
Based on driver/char/serial.c by Linus Torvalds, Theodore Ts'o.

SUNIX serial card designed with SUNIX UART controller and compatible with 16C950 UART specification.

---
 char/snx/snx_main.c   | 1219 ++++++++++++
 char/snx/snx_serial.c | 4263 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 5482 insertions(+)
 create mode 100644 char/snx/snx_main.c
 create mode 100644 char/snx/snx_serial.c
char/snx/snx_main.c new file +1219 -0
new file mode 100644
index 00000000..dd4fba1b
@@ -0,0 +1,1219 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "snx_common.h"
+#include "driver_extd.h"
+
+MODULE_AUTHOR(SNX_DRIVER_AUTHOR);
+MODULE_DESCRIPTION(SNX_DRIVER_DESC);
+MODULE_LICENSE("GPL");
+
+char snx_ser_ic_table[SNX_SER_PORT_MAX_UART][10] = {
+	{"UNKNOWN"},
+	{"SUN1889"},
+	{"SUN1699"},
+	{"SUNMATX"},
+	{"SUN1999"}
+};
+
+char snx_par_ic_table[SNX_PAR_PORT_MAX_UART][10] = {
+	{"UNKNOWN"},
+	{"SUN1888"},
+	{"SUN1689"},
+	{"SUNMATX"},
+	{"SUN1999"}
+};
+
+char snx_port_remap[2][10] = {
+	{"NON-REMAP"},
+	{"REMAP"}
+};
+
+enum{
+// golden-serial
+	GOLDEN_BOARD_TEST = 0,
+
+// matrix-serial
+	MATRIX_BOARD_P1002,
+	MATRIX_BOARD_P1004,
+	MATRIX_BOARD_P1008,
+	MATRIX_BOARD_P1016,
+	MATRIX_BOARD_P2002,
+	MATRIX_BOARD_P2004,
+	MATRIX_BOARD_P2008,
+	MATRIX_BOARD_P3002,
+	MATRIX_BOARD_P3004,
+	MATRIX_BOARD_P3008,
+
+// sun1999-serial RS232
+	SUN1999_BOARD_5027A,
+	SUN1999_BOARD_5037A,
+	SUN1999_BOARD_5056A,
+	SUN1999_BOARD_5066A,
+	SUN1999_BOARD_5016A,
+
+	//sun1999-multi I/O
+	SUN1999_BOARD_5069A,
+	SUN1999_BOARD_5079A,
+	SUN1999_BOARD_5099A,
+
+	//sun1999-parallel
+
+	SUN1999_BOARD_5008A,
+
+	//sun1999-serial RS422/485
+	SUN1999_BOARD_P2102,
+	SUN1999_BOARD_P2104,
+	SUN1999_BOARD_P2108,
+	SUN1999_BOARD_P2116,
+
+	//sun1999 3_in_1
+	SUN1999_BOARD_P3104,
+	SUN1999_BOARD_P3108,
+
+	//cash drawer card
+	SUN1999_BOARD_CASH_2S,
+	SUN1999_BOARD_CASH_4S,
+
+	//DIO
+	SUN1999_BOARD_DIO0802,
+	SUN1999_BOARD_DIO1604,
+	SUN1999_BOARD_DIO3204,
+
+};
+
+static  struct pci_device_id	sunix_pci_board_id[] = {
+// golden-serial
+	{VENID_GOLDEN,	DEVID_G_SERIAL,	SUBVENID_GOLDEN,
+		SUBDEVID_TEST,	0,	0,	GOLDEN_BOARD_TEST},
+
+// matrix-serial
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1002,	0,	0,	MATRIX_BOARD_P1002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1004,	0,	0,	MATRIX_BOARD_P1004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1008,	0,	0,	MATRIX_BOARD_P1008},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1016,	0,	0,	MATRIX_BOARD_P1016},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2002,	0,	0,	MATRIX_BOARD_P2002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2004,	0,	0,	MATRIX_BOARD_P2004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2008,	0,	0,	MATRIX_BOARD_P2008},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3002,	0,	0,	MATRIX_BOARD_P3002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3004,	0,	0,	MATRIX_BOARD_P3004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3008,	0,	0,	MATRIX_BOARD_P3008},
+
+	// sun1999-serial RS232
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5027A,	0,	0,	SUN1999_BOARD_5027A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5037A,	0,	0,	SUN1999_BOARD_5037A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5056A,	0,	0,	SUN1999_BOARD_5056A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5066A,	0,	0,	SUN1999_BOARD_5066A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5016A,	0,	0,	SUN1999_BOARD_5016A},
+
+	// sun1999-multi I/O
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5069A,	0,	0,	SUN1999_BOARD_5069A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5079A,	0,	0,	SUN1999_BOARD_5079A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5099A,	0,	0,	SUN1999_BOARD_5099A},
+
+	// sun1999-parallel
+	{VENID_SUN1999,	DEVID_S_PARALL,	SUBVENID_SUN1999,
+		SUBDEVID_5008A,	0,	0,	SUN1999_BOARD_5008A},
+
+	// sun1999-serial RS422/485
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2102,	0,	0,	SUN1999_BOARD_P2102},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2104,	0,	0,	SUN1999_BOARD_P2104},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2108,	0,	0,	SUN1999_BOARD_P2108},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2116,	0,	0,	SUN1999_BOARD_P2116},
+
+	// sun1999 3_in_1
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P3104,	0,	0,	SUN1999_BOARD_P3104},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P3108,	0,	0,	SUN1999_BOARD_P3108},
+
+	//cash drawer card  2S
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_CASH_2S, 0,	0,	SUN1999_BOARD_CASH_2S},
+
+	//cash drawer card  4S
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_CASH_4S, 0,	0,	SUN1999_BOARD_CASH_4S},
+
+	//DIO
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO0802, 0,	0,	SUN1999_BOARD_DIO0802},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO1604, 0,	0,	SUN1999_BOARD_DIO1604},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO3204, 0,	0,	SUN1999_BOARD_DIO3204},
+
+	{0}
+};
+MODULE_DEVICE_TABLE(pci, sunix_pci_board_id);
+
+struct sunix_board			sunix_board_table[SNX_BOARDS_MAX];
+struct sunix_ser_port	    sunix_ser_table[SNX_SER_TOTAL_MAX + 1];
+struct sunix_par_port	    sunix_par_table[SNX_PAR_TOTAL_MAX];
+
+static int snx_ser_port_total_cnt;
+static int snx_par_port_total_cnt;
+
+int snx_board_count;
+
+static struct snx_ser_driver	sunix_ser_reg = {
+	.dev_name = "ttySNX",
+	.major = 0,
+	.minor = 0,
+	.nr = (SNX_SER_TOTAL_MAX + 1),
+};
+
+static irqreturn_t sunix_interrupt(int irq, void *dev_id)
+{
+	struct sunix_ser_port *sp = NULL;
+	struct sunix_par_port *pp = NULL;
+	struct sunix_board *sb = NULL;
+	int i;
+	int status = 0;
+
+	int handled = IRQ_NONE;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+
+		if (dev_id == &(sunix_board_table[i])) {
+			sb = dev_id;
+			break;
+		}
+	}
+
+	if (i == SNX_BOARDS_MAX)
+		status = 1;
+
+	if (!sb)
+		status = 1;
+
+	if (sb->board_enum <= 0)
+		status = 1;
+
+	if (status != 0)
+		return handled;
+
+	if ((sb->ser_port > 0) && (sb->ser_isr != NULL)) {
+		sp = &sunix_ser_table[sb->ser_port_index];
+
+		if (!sp)
+			status = 1;
+
+		status = sb->ser_isr(sb, sp);
+	}
+
+	if ((sb->par_port > 0) && (sb->par_isr != NULL)) {
+		pp = &sunix_par_table[sb->par_port_index];
+
+		if (!pp)
+			status = 1;
+
+		status = sb->par_isr(sb, pp);
+	}
+
+	if (status != 0)
+		return handled;
+
+	handled = IRQ_HANDLED;
+	return handled;
+}
+
+static int snx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+	return 0;
+}
+
+static int snx_suspend_one(struct pci_dev *pdev, pm_message_t state)
+{
+	return  0;
+}
+
+static int snx_set_port_termios(struct snx_ser_state *state)
+{
+	struct tty_struct *tty = state->info->tty;
+	struct SNXTERMIOS *termios;
+
+	int retval = 0;
+
+	termios = &tty->termios;
+
+	retval = snx_ser_startup(state, 0);
+
+	if (retval == 0)
+		snx_ser_update_termios(state);
+
+	return 0;
+}
+
+static int snx_resume_port_termios(struct snx_ser_info *info)
+{
+	struct snx_ser_state *state = NULL;
+	struct tty_struct *tty = info->tty;
+
+	state = tty->driver_data;
+	snx_set_port_termios(state);
+
+	return 0;
+}
+
+
+static int snx_resume_port(struct sunix_ser_port *sp)
+{
+	struct snx_ser_port *port = &sp->port;
+	struct snx_ser_info *info = port->info;
+
+	if (info)
+		snx_resume_port_termios(info);
+
+	return 0;
+}
+
+static int snx_resume_one(struct pci_dev *pdev)
+{
+	struct sunix_board *sb = pci_get_drvdata(pdev);
+	struct sunix_ser_port *sp = NULL;
+	int j;
+
+	if (sb == NULL)
+		return 0;
+
+	for (j = 0; j < sb->ser_port; j++) {
+		sp = &sunix_ser_table[j];
+
+		if (sp == NULL)
+			return 0;
+
+		if (sp->port.suspended == 1)
+			snx_resume_port(sp);
+	}
+
+	return 0;
+}
+
+
+static int sunix_pci_board_probe(void)
+{
+	struct sunix_board *sb;
+	struct pci_dev *pdev = NULL;
+	struct pci_dev *pdev_array[4] = {NULL, NULL, NULL, NULL};
+
+	int sunix_pci_board_id_cnt;
+	int tablecnt;
+	int boardcnt;
+	int i;
+	unsigned short int sub_device_id;
+	unsigned short int device_part_number;
+	unsigned int bar3_base_add;
+
+	int status;
+	unsigned int bar3_Byte5;
+	unsigned int bar3_Byte6;
+	unsigned int bar3_Byte7;
+	unsigned int oem_id;
+	unsigned char uart_type;
+	unsigned char gpio_type;
+	unsigned char gpio_card_type;
+	int gpio_ch_cnt;
+
+	// clear and init some variable
+	memset(sunix_board_table, 0, SNX_BOARDS_MAX *
+	sizeof(struct sunix_board));
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sunix_board_table[i].board_enum = -1;
+		sunix_board_table[i].board_number = -1;
+	}
+
+	sunix_pci_board_id_cnt =
+	(sizeof(sunix_pci_board_id) / sizeof(sunix_pci_board_id[0])) - 1;
+
+	// search matrix serial board
+	pdev = NULL;
+	tablecnt = 0;
+	boardcnt = 0;
+	sub_device_id = 0;
+	status = 0;
+
+	while (tablecnt < sunix_pci_board_id_cnt) {
+
+		pdev = pci_get_device(VENID_MATRIX, DEVID_M_SERIAL, pdev);
+
+		if (pdev == NULL) {
+			tablecnt++;
+			continue;
+		}
+
+		if ((tablecnt > 0) && ((pdev == pdev_array[0]) ||
+		(pdev == pdev_array[1]) ||
+		(pdev == pdev_array[2]) ||
+		(pdev == pdev_array[3]))) {
+			continue;
+		}
+
+		pci_read_config_word(pdev, 0x2e, &sub_device_id);
+
+		if (sub_device_id == 0) {
+			status = -EIO;
+			return status;
+		}
+
+		if (sub_device_id != sunix_pci_board_id[tablecnt].subdevice)
+			continue;
+		if (pdev == NULL) {
+			pr_err("SNX Error: PCI device object is NULL !\n");
+			status = -EIO;
+			return status;
+
+		} else {
+
+			status = pci_enable_device(pdev);
+
+			if (status != 0) {
+				pr_err("SNX Error: SUNIX Board Enable Fail !\n\n");
+				status = -ENXIO;
+				return status;
+			}
+		}
+
+		boardcnt++;
+		if (boardcnt > SNX_BOARDS_MAX) {
+			pr_err("SNX Error: Support Four Boards In Maximum !\n");
+			status = -ENOSPC;
+			return status;
+		}
+
+		sb = &sunix_board_table[boardcnt-1];
+		pdev_array[boardcnt-1] = pdev;
+		sb->pdev = pdev;
+		sb->bus_number = pdev->bus->number;
+		sb->dev_number = PCI_SLOT(pdev->devfn);
+		sb->board_enum = (int)sunix_pci_board_id[tablecnt].driver_data;
+		sb->pb_info = snx_pci_board_conf[sb->board_enum];
+		sb->board_flag = sb->pb_info.board_flag;
+		sb->board_number = boardcnt - 1;
+	}
+
+	// search sun1999 muti I/O board
+	pdev = NULL;
+	tablecnt = 0;
+	sub_device_id = 0;
+	status = 0;
+	device_part_number = 0;
+	bar3_base_add = 0;
+	bar3_Byte5 = 0;
+	bar3_Byte6 = 0;
+	bar3_Byte7 = 0;
+	oem_id = 0;
+	uart_type = 0;
+	gpio_type = 0;
+	gpio_card_type = 0;
+	gpio_ch_cnt = 0;
+
+	while (tablecnt < sunix_pci_board_id_cnt) {
+
+		pdev = pci_get_device(VENID_SUN1999, DEVID_S_SERIAL, pdev);
+
+		if (pdev == NULL) {
+			tablecnt++;
+			continue;
+		}
+
+		if ((tablecnt > 0) &&
+		((pdev == pdev_array[0]) ||
+		(pdev == pdev_array[1]) ||
+		(pdev == pdev_array[2]) ||
+		(pdev == pdev_array[3]))) {
+			continue;
+		}
+
+		pci_read_config_word(pdev, 0x2e, &sub_device_id);
+
+		if (sub_device_id == 0) {
+			status = -EIO;
+			return status;
+		}
+
+		if (sub_device_id != sunix_pci_board_id[tablecnt].subdevice)
+			continue;
+
+		if (pdev == NULL) {
+			pr_err("SNX Error: PCI device object is an NULL pointer !\n\n");
+			status = -EIO;
+			return status;
+
+		} else {
+
+			status = pci_enable_device(pdev);
+
+			if (status != 0) {
+				pr_err("SNX Error: SUNIX Board Enable Fail !\n\n");
+				status = -ENXIO;
+				return status;
+			}
+		}
+
+		bar3_base_add = pci_resource_start(pdev, 3);
+		device_part_number = inb(bar3_base_add + 5);
+		bar3_Byte5 = device_part_number;
+		bar3_Byte6 = inb(bar3_base_add + 0x06);
+		bar3_Byte7 = inb(bar3_base_add + 0x07);
+		gpio_card_type = ((bar3_Byte7 & 0x18)>>3);
+		oem_id = (bar3_Byte5 | (bar3_Byte6 << 8) | (bar3_Byte7 << 16));
+		uart_type = ((bar3_Byte5 & 0xc0)>>6);
+		gpio_ch_cnt = ((bar3_Byte7 & 0x60)>>5);
+		gpio_type = ((bar3_Byte7 & 0x80)>>7);
+
+		if ((gpio_ch_cnt == 0x00) && (gpio_card_type == 0x01))
+			gpio_ch_cnt = 6;
+		else if ((gpio_ch_cnt == 0x00) && (gpio_card_type == 0x02))
+			gpio_ch_cnt = 8;
+		else if (gpio_ch_cnt == 0x01)
+			gpio_ch_cnt = 16;
+		else if (gpio_ch_cnt == 0x02)
+			gpio_ch_cnt = 32;
+
+	if (device_part_number != snx_pci_board_conf[tablecnt].part_number)
+		continue;
+	else if (gpio_card_type != snx_pci_board_conf[tablecnt].card_type)
+		continue;
+	else if (gpio_ch_cnt != snx_pci_board_conf[tablecnt].gpio_ch_cnt)
+		continue;
+	else
+		pr_err("\n");
+
+		boardcnt++;
+		if (boardcnt > SNX_BOARDS_MAX) {
+			pr_err("SNX Error: Support Four Boards In Maximum !\n");
+			status = -ENOSPC;
+			return status;
+		}
+
+		sb = &sunix_board_table[boardcnt-1];
+		pdev_array[boardcnt-1] = pdev;
+		sb->pdev = pdev;
+		sb->bus_number = pdev->bus->number;
+		sb->dev_number = PCI_SLOT(pdev->devfn);
+		sb->board_enum = (int)sunix_pci_board_id[tablecnt].driver_data;
+		sb->pb_info = snx_pci_board_conf[sb->board_enum];
+		sb->board_flag = sb->pb_info.board_flag;
+		sb->board_number = boardcnt - 1;
+		sb->oem_id = oem_id;
+		sb->uart_cnt = sb->pb_info.num_serport;
+		sb->gpio_chl_cnt = gpio_ch_cnt;
+		sb->board_uart_type = uart_type;
+		sb->board_gpio_card_type = gpio_card_type;
+		sb->board_gpio_type = gpio_type;
+	}
+
+	// search SUN1999 parallel board
+	pdev = NULL;
+	tablecnt = 0;
+	sub_device_id = 0;
+	status = 0;
+
+	while (tablecnt < sunix_pci_board_id_cnt) {
+
+		pdev = pci_get_device(VENID_SUN1999, DEVID_S_PARALL, pdev);
+
+		if (pdev == NULL) {
+			tablecnt++;
+			continue;
+		}
+
+		if ((tablecnt > 0) &&
+		((pdev == pdev_array[0]) ||
+		(pdev == pdev_array[1]) ||
+		(pdev == pdev_array[2]) ||
+		(pdev == pdev_array[3]))) {
+			continue;
+		}
+
+		pci_read_config_word(pdev, 0x2e, &sub_device_id);
+
+		if (sub_device_id == 0) {
+			status = -EIO;
+			return status;
+		}
+
+		if (sub_device_id != sunix_pci_board_id[tablecnt].subdevice)
+			continue;
+
+		if (pdev == NULL) {
+			pr_err("SNX Error: PCI device object is an NULL pointer !\n\n");
+			status = -EIO;
+			return status;
+
+		} else {
+
+			status = pci_enable_device(pdev);
+
+			if (status != 0) {
+				pr_err("SNX Error: SUNIX Board Enable Fail !\n\n");
+				status = -ENXIO;
+				return status;
+			}
+		}
+
+		boardcnt++;
+		if (boardcnt > SNX_BOARDS_MAX) {
+			pr_err("\n");
+			pr_err("SNX Error: SUNIX Driver Module Support Four Boards In Maximum !\n\n");
+			status = -ENOSPC;
+			return status;
+		}
+
+		sb = &sunix_board_table[boardcnt-1];
+		pdev_array[boardcnt-1] = pdev;
+		sb->pdev = pdev;
+		sb->bus_number = pdev->bus->number;
+		sb->dev_number = PCI_SLOT(pdev->devfn);
+		sb->board_enum = (int)sunix_pci_board_id[tablecnt].driver_data;
+		sb->pb_info = snx_pci_board_conf[sb->board_enum];
+		sb->board_flag = sb->pb_info.board_flag;
+		sb->board_number = boardcnt - 1;
+	}
+
+	// print info
+	if (boardcnt == 0) {
+		pr_err("SNX Info : No SUNIX Multi-I/O Board Found !\n\n");
+		status = -ENXIO;
+		return status;
+
+	} else {
+
+		for (i = 0; i < SNX_BOARDS_MAX; i++) {
+			sb = &sunix_board_table[i];
+
+			if (sb->board_enum > 0) {
+				pr_err("\n");
+
+			if ((sb->pb_info.num_serport > 0) &&
+				(sb->pb_info.num_parport > 0)) {
+				pr_err("SNX Info : Found SUNIX %s Series Board (%dS%dP),\n",
+				sb->pb_info.board_name,
+				sb->pb_info.num_serport,
+				sb->pb_info.num_parport);
+			} else if ((sb->pb_info.num_serport) > 0) {
+				pr_err("SNX Info : Found SUNIX %s Series Board (%dS),\n",
+				sb->pb_info.board_name,
+				sb->pb_info.num_serport);
+			} else {
+				pr_err("SNX Info : Found SUNIX %s Series Board (%dP),\n",
+				sb->pb_info.board_name,
+				sb->pb_info.num_parport);
+			}
+				pr_err(" bus number:%d, device number:%d\n\n",
+				sb->bus_number, sb->dev_number);
+			}
+		}
+		snx_board_count = boardcnt;
+	}
+
+	return status;
+}
+
+
+static int sunix_get_pci_board_conf(void)
+{
+	struct sunix_board *sb = NULL;
+	struct pci_dev *pdev = NULL;
+	int status = 0;
+	int i;
+	int j;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb->board_enum > 0) {
+			pdev = sb->pdev;
+			sb->ports = sb->pb_info.num_serport +
+				sb->pb_info.num_parport;
+			sb->ser_port = sb->pb_info.num_serport;
+			sb->par_port = sb->pb_info.num_parport;
+			snx_ser_port_total_cnt = snx_ser_port_total_cnt +
+				sb->ser_port;
+			snx_par_port_total_cnt = snx_par_port_total_cnt +
+				sb->par_port;
+
+			if (snx_ser_port_total_cnt > SNX_SER_TOTAL_MAX) {
+				pr_err("SNX Error: Too much serial port\n");
+				status = -EIO;
+				return status;
+			}
+
+			if (snx_par_port_total_cnt > SNX_PAR_SUPPORT_MAX) {
+				pr_err("SNX Error: Too much parallel port\n");
+				status = -EIO;
+				return status;
+			}
+
+			for (j = 0; j < SNX_PCICFG_BAR_TOTAL; j++)
+				sb->bar_addr[j] = pci_resource_start(pdev, j);
+
+
+			sb->irq = sb->pdev->irq;
+
+			if (sb->irq <= 0) {
+				pr_err("SNX Error: SUNIX Board %s Series (bus:%d device:%d), in configuartion space, irq isn't valid !\n\n",
+				sb->pb_info.board_name,
+				sb->bus_number,
+				sb->dev_number);
+
+				status = -EIO;
+				return status;
+			}
+		}
+	}
+
+	return status;
+}
+
+
+static int sunix_assign_resource(void)
+{
+	struct sunix_board *sb = NULL;
+	struct sunix_ser_port *sp = NULL;
+	struct sunix_par_port *pp = NULL;
+
+	int status = 0;
+	int i;
+	int j;
+	int k;
+	int ser_n;
+	int ser_port_index = 0;
+
+	memset(sunix_ser_table, 0, (SNX_SER_TOTAL_MAX + 1) *
+	sizeof(struct sunix_ser_port));
+
+	memset(sunix_par_table, 0, (SNX_PAR_TOTAL_MAX) *
+	sizeof(struct sunix_par_port));
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb->board_enum > 0) {
+			if (sb->ser_port > 0) {
+				sb->vector_mask = 0;
+
+		// assign serial port resource
+		ser_n = sb->ser_port_index = ser_port_index;
+
+		sp = &sunix_ser_table[ser_n];
+
+		if (sp == NULL) {
+			status = -ENXIO;
+			pr_err("SNX Error: Serial port table address error !\n");
+			return status;
+		}
+
+		for (j = 0; j < sb->ser_port; j++, ser_n++, sp++) {
+			sp->port.chip_flag = sb->pb_info.port[j].chip_flag;
+			sp->port.iobase = sb->bar_addr[
+			sb->pb_info.port[j].bar1] +
+			sb->pb_info.port[j].offset1;
+
+			if ((sb->board_flag & BOARDFLAG_REMAP) ==
+			BOARDFLAG_REMAP) {
+				sp->port.vector = 0;
+				sb->vector_mask = 0x00;
+			} else {
+				sp->port.vector = sb->bar_addr[
+				sb->pb_info.intr_vector_bar] +
+				sb->pb_info.intr_vector_offset;
+				sb->vector_mask |= (1 << j);
+			}
+		}
+
+			ser_port_index = ser_port_index + sb->ser_port;
+		}
+
+
+		// assign parallel port resource
+		if (sb->par_port > 0) {
+			k = 0;
+
+			for (j = 0; j < SNX_PAR_TOTAL_MAX; j++) {
+				if ((k + 1) > sb->par_port)
+					break;
+
+				if (j >= SNX_PAR_TOTAL_MAX) {
+					status = -EACCES;
+					pr_err("SNX Error: Too much parallel port!\n");
+					return status;
+				}
+
+				pp = &sunix_par_table[j];
+
+				if (pp == NULL) {
+					status = -ENXIO;
+					pr_err("SNX Error: Parallel port table address error !\n");
+					return status;
+				}
+
+				if (pp->chip_flag != SUNNONE_HWID) {
+					continue;
+				} else {
+					pp->chip_flag = sb->pb_info.port[k +
+					sb->ser_port].chip_flag;
+
+					pp->base = sb->bar_addr[
+					sb->pb_info.port[k +
+					sb->ser_port].bar1] +
+					sb->pb_info.port[k +
+					sb->ser_port].offset1;
+
+					pp->base_hi = sb->bar_addr[
+					sb->pb_info.port[k +
+					sb->ser_port].bar2] +
+					sb->pb_info.port[k +
+					sb->ser_port].offset2;
+
+					pp->bus_number = sb->bus_number;
+					pp->dev_number = sb->dev_number;
+					pp->board_enum = sb->board_enum;
+					k++;
+				}
+			}
+		}
+		}
+	}
+
+	return status;
+}
+
+
+static int sunix_ser_port_table_init(void)
+{
+	struct sunix_board *sb = NULL;
+	struct sunix_ser_port *sp = NULL;
+	int status = 0;
+	int i;
+	int j;
+	int n;
+	int AHDC_State = 0;
+	int RS422_State = 0;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+	if (sb == NULL) {
+		status = -ENXIO;
+		pr_err("SNX Error: Board table pointer error !\n");
+		return status;
+	}
+
+	if ((sb->board_enum > 0) && (sb->ser_port > 0)) {
+		n = sb->ser_port_index;
+		sp = &sunix_ser_table[n];
+
+		if (sp == NULL) {
+			status = -ENXIO;
+			pr_err("SNX Error: Serial port table pointer error !\n");
+			return status;
+		}
+
+		for (j = 0; j < sb->ser_port; j++, n++, sp++) {
+			if (j < 4) {
+				AHDC_State = inb(sb->bar_addr[3]+2) & 0x0F &
+						(0x01 << (((j+1)-1) % 4));
+				RS422_State = inb(sb->bar_addr[3]+3) &
+				0xF0 & (0x10 << (((j+1)-1) % 4));
+			} else if (j < 8) {
+				AHDC_State = inb(sb->bar_addr[1] + 0x32) &
+				0x0F & (0x01 << (((j+1)-1) % 4));
+				RS422_State = inb(sb->bar_addr[1] + 0x33) &
+				0xF0 & (0x10 << (((j+1)-1) % 4));
+			}
+
+			RS422_State = ((RS422_State & 0xF0) >> 4);
+			sp->port.AHDC_State = AHDC_State >> (((j+1)-1)%4);
+			sp->port.RS422_State = RS422_State >> (((j+1)-1)%4);
+
+			sp->port.board_enum	= sb->board_enum;
+			sp->port.bus_number	= sb->bus_number;
+			sp->port.dev_number	= sb->dev_number;
+			sp->port.baud_base = 921600;
+			sp->port.pb_info = sb->pb_info;
+			sp->port.irq = sb->irq;
+			sp->port.line = n;
+			sp->port.uartclk = sp->port.baud_base * 16;
+			sp->port.iotype = SNX_UPIO_PORT;
+			sp->port.flags = ASYNC_SHARE_IRQ;
+			sp->port.ldisc_stop_rx = 0;
+			spin_lock_init(&sp->port.lock);
+
+			if (sp->port.chip_flag == SUN1889_HWID) {
+				sp->port.snx_type = SNX_SER_PORT_SUN1889;
+				sp->port.type = PORT_SER_16650V2;
+				sp->port.fifosize = SUN1889_FIFOSIZE_SET;
+				sp->port.rx_trigger = SUN1889_TRIGGER_LEVEL_SET;
+			} else if (sp->port.chip_flag == SUN1699_HWID) {
+				sp->port.snx_type = SNX_SER_PORT_SUN1699;
+				sp->port.type = PORT_SER_16650V2;
+				sp->port.fifosize = SUN1699_FIFOSIZE_SET;
+				sp->port.rx_trigger = SUN1699_TRIGGER_LEVEL_SET;
+			} else if (sp->port.chip_flag == SUNMATX_HWID) {
+				sp->port.snx_type = SNX_SER_PORT_SUNMATX;
+				sp->port.type = PORT_SER_16750;
+				sp->port.fifosize = SUNMATX_FIFOSIZE_SET;
+				sp->port.rx_trigger = SUNMATX_TRIGGER_LEVEL_SET;
+			} else if (sp->port.chip_flag == SUN1999_HWID) {
+				sp->port.snx_type = SNX_SER_PORT_SUN1999;
+				sp->port.type = PORT_SER_16750;
+				sp->port.fifosize = SUN1999_FIFOSIZE_SET;
+				sp->port.rx_trigger = SUN1999_TRIGGER_LEVEL_SET;
+			} else {
+				sp->port.snx_type = SNX_SER_PORT_UNKNOWN;
+				sp->port.type = PORT_SER_16450;
+				sp->port.fifosize = DEFAULT_FIFOSIZE;
+				sp->port.rx_trigger = DEFAULT_TRIGGER_LEVEL;
+			}
+
+
+			if ((sb->pb_info.board_flag &
+				BOARDFLAG_REMAP) == BOARDFLAG_REMAP) {
+				sp->port.vector_mask = 0;
+				sp->port.port_flag = PORTFLAG_REMAP;
+			} else {
+				sp->port.vector_mask = sb->vector_mask;
+				sp->port.port_flag = PORTFLAG_NONE;
+			}
+
+			if ((sb->pb_info.board_flag &
+				BOARDFLAG_16PORTS) ==	BOARDFLAG_16PORTS)
+				sp->port.port_flag |= PORTFLAG_16PORTS;
+
+
+			sp->port.setserial_flag = SNX_SER_BAUD_NOTSETSER;
+			}
+
+			sb->ser_isr = sunix_ser_interrupt;
+		} else {
+			sb->ser_isr = NULL;
+		}
+	}
+
+
+	// release io resource
+	for (i = 0; i < SNX_SER_TOTAL_MAX; i++) {
+		sp = &sunix_ser_table[i];
+
+		if (sp->port.iobase > 0)
+			release_region(sp->port.iobase, SNX_SER_ADDRESS_LENGTH);
+
+	}
+	return status;
+}
+
+
+static int sunix_par_port_table_init(void)
+{
+	struct sunix_board *sb = NULL;
+	struct sunix_par_port *pp = NULL;
+	int status = 0;
+	int i;
+	int j;
+	int k;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb == NULL) {
+			status = -ENXIO;
+			pr_err("SNX Error: Board table pointer error !\n");
+			return status;
+		}
+
+		if ((sb->board_enum > 0) && (sb->par_port > 0)) {
+			k = 0;
+			for (j = 0; j < SNX_PAR_TOTAL_MAX; j++) {
+				pp = &sunix_par_table[j];
+
+				if (pp == NULL) {
+					status = -ENXIO;
+		pr_err("SNX Error: Parallel port tablepointer error\n");
+					return status;
+				}
+
+				if ((k + 1) > sb->par_port)
+					break;
+
+				if ((pp->bus_number == sb->bus_number) &&
+					(pp->dev_number == sb->dev_number) &&
+					(pp->board_enum	== sb->board_enum)) {
+
+					pp->pb_info = sb->pb_info;
+					//pp->irq = sb->irq;
+					pp->irq	= PARPORT_IRQ_NONE;
+					pp->portnum = j;
+
+					if (pp->chip_flag == SUN1888_HWID) {
+						pp->snx_type =
+						SNX_PAR_PORT_SUN1888;
+					} else if (pp->chip_flag ==
+						SUN1689_HWID) {
+						pp->snx_type =
+						SNX_PAR_PORT_SUN1689;
+					} else if (pp->chip_flag ==
+						SUNMATX_HWID) {
+						pp->snx_type =
+						SNX_PAR_PORT_SUNMATX;
+					} else if (pp->chip_flag ==
+						SUN1999_HWID) {
+						pp->snx_type =
+						SNX_PAR_PORT_SUN1999;
+					} else {
+						pp->snx_type =
+						SNX_PAR_PORT_UNKNOWN;
+					}
+
+					if ((sb->pb_info.board_flag &
+						BOARDFLAG_REMAP) ==
+						BOARDFLAG_REMAP) {
+						pp->port_flag = PORTFLAG_REMAP;
+					} else {
+						pp->port_flag = PORTFLAG_NONE;
+					}
+					sb->par_isr = NULL;
+					k++;
+				}
+			}
+		}
+	}
+
+
+	// release io resource
+	for (i = 0; i < SNX_PAR_TOTAL_MAX; i++) {
+		pp = &sunix_par_table[i];
+
+		if (pp->base > 0) {
+			release_region(pp->base, SNX_PAR_ADDRESS_LENGTH);
+
+
+			release_region(pp->base, SNX_PAR_STD_ADDR_LENGTH);
+
+			release_region(pp->base +
+			SNX_PAR_STD_ADDR_LENGTH,
+			SNX_PAR_ETD_ADDR_LENGTH);
+
+			if (pp->base_hi > 0) {
+				release_region(pp->base_hi,
+				SNX_PAR_ADDRESS_LENGTH);
+
+				release_region(pp->base_hi,
+				SNX_PAR_STD_ADDR_LENGTH);
+
+				release_region(pp->base_hi +
+				SNX_PAR_STD_ADDR_LENGTH,
+				SNX_PAR_ETD_ADDR_LENGTH);
+			}
+		}
+	}
+
+	return status;
+}
+
+int sunix_register_irq(void)
+{
+	struct sunix_board *sb = NULL;
+	int status = 0;
+	int i;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb == NULL) {
+			status = -ENXIO;
+			pr_err("SNX Error: Board table pointer error !\n");
+			return status;
+		}
+
+		if (sb->board_enum > 0) {
+			status = request_irq(sb->irq,
+			sunix_interrupt,
+			IRQF_SHARED,
+			"snx", sb);
+
+			if (status) {
+				pr_err("SNX Error: SUNIX Multi-I/O %s Board(bus:%d device:%d), request\n",
+				sb->pb_info.board_name,
+				sb->bus_number,
+				sb->dev_number);
+				pr_err("           IRQ %d fail, IRQ %d may be conflit with another device.\n",
+				sb->irq, sb->irq);
+				return status;
+			}
+		}
+	}
+
+	return status;
+}
+
+
+void sunix_release_irq(void)
+{
+	struct sunix_board *sb = NULL;
+	int i;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb->board_enum > 0)
+			free_irq(sb->irq, sb);
+	}
+}
+
+static struct pci_driver snx_pci_driver = {
+	.name			= "snx",
+	.probe			= snx_pci_probe,
+	.suspend		= snx_suspend_one,
+	.resume			= snx_resume_one,
+	.id_table		= sunix_pci_board_id,
+};
+
+static int __init snx_init(void)
+{
+	int status = 0;
+
+	pr_err("SNX Info : Loading SUNIX Multi-I/O Board Driver Module\n");
+
+	snx_ser_port_total_cnt = snx_par_port_total_cnt = 0;
+
+	status = sunix_pci_board_probe();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_get_pci_board_conf();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_assign_resource();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_ser_port_table_init();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_par_port_table_init();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_register_irq();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_ser_register_driver(&sunix_ser_reg);
+	if (status != 0)
+		goto step2_fail;
+
+	status = sunix_ser_register_ports(&sunix_ser_reg);
+	if (status != 0)
+		goto step3_fail;
+
+	status = pci_register_driver(&snx_pci_driver);
+	if (status != 0)
+		goto step7_fail;
+
+	if (snx_par_port_total_cnt > 0) {
+		status = sunix_par_parport_init();
+		if (status != 0)
+			goto step4_fail;
+
+		status = sunix_par_ppdev_init();
+		if (status != 0)
+			goto step5_fail;
+
+		status = sunix_par_lp_init();
+		if (status != 0)
+			goto step6_fail;
+	}
+
+#if SNX_DBG
+	sunix_debug();
+#endif
+
+	return status;
+
+
+	if (snx_par_port_total_cnt > 0) {
+step7_fail:
+
+		pci_unregister_driver(&snx_pci_driver);
+step6_fail:
+
+		sunix_par_ppdev_exit();
+
+
+step5_fail:
+
+		sunix_par_parport_exit();
+
+
+step4_fail:
+
+		sunix_ser_unregister_ports(&sunix_ser_reg);
+	}
+
+step3_fail:
+
+	sunix_ser_unregister_driver(&sunix_ser_reg);
+
+
+step2_fail:
+
+	sunix_release_irq();
+
+
+step1_fail:
+
+	pr_err("SNX Error: Couldn't Loading SUNIX Multi-I/O Board Driver Module correctly,\n");
+	pr_err("           please reboot system and try again. If still can't loading driver,\n");
+	pr_err("           contact support.\n\n");
+
+	return status;
+}
+
+
+static void __exit snx_exit(void)
+{
+	if (snx_par_port_total_cnt > 0) {
+		sunix_par_lp_exit();
+
+		sunix_par_ppdev_exit();
+
+		sunix_par_parport_exit();
+	}
+
+	sunix_ser_unregister_ports(&sunix_ser_reg);
+
+	sunix_ser_unregister_driver(&sunix_ser_reg);
+
+	sunix_release_irq();
+	pci_unregister_driver(&snx_pci_driver);
+	pr_err("SNX Info : Unload SUNIX Multi-I/O Board Driver Module Done.\n");
+}
+
+module_init(snx_init);
+module_exit(snx_exit);
+
char/snx/snx_serial.c new file +4263 -0

1,781 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.

#1 lore raw

On 08.03.19 13:34, Morris Ku wrote:

Hi,

<snip>

char/snx/snx_main.c

Quoted 1 line
char/snx/snx_main.c +0 -0

if it's a serial card, shouldn't it go to drivers/tty/serial/snx/ ?

<snip>

Quoted 10 lines
char/snx/snx_main.c +10 -0
+char snx_ser_ic_table[SNX_SER_PORT_MAX_UART][10] = {
+	{"UNKNOWN"},
+	{"SUN1889"},
+	{"SUN1699"},
+	{"SUNMATX"},
+	{"SUN1999"}
+};
+
+char snx_par_ic_table[SNX_PAR_PORT_MAX_UART][10] = {
+	{"UNKNOWN"},

char/snx/snx_main.c line 28

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	{"SUN1888"},
+	{"SUN1689"},
+	{"SUNMATX"},
+	{"SUN1999"}
+};
+
+char snx_port_remap[2][10] = {
+	{"NON-REMAP"},
+	{"REMAP"}
+};

can't these be const static ?

Quoted 21 lines
char/snx/snx_main.c +21 -0
+
+enum{
+// golden-serial
+	GOLDEN_BOARD_TEST = 0,
+
+// matrix-serial
+	MATRIX_BOARD_P1002,
+	MATRIX_BOARD_P1004,
+	MATRIX_BOARD_P1008,
+	MATRIX_BOARD_P1016,
+	MATRIX_BOARD_P2002,
+	MATRIX_BOARD_P2004,
+	MATRIX_BOARD_P2008,
+	MATRIX_BOARD_P3002,
+	MATRIX_BOARD_P3004,
+	MATRIX_BOARD_P3008,
+
+// sun1999-serial RS232
+	SUN1999_BOARD_5027A,
+	SUN1999_BOARD_5037A,
+	SUN1999_BOARD_5056A,

char/snx/snx_main.c line 59

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	SUN1999_BOARD_5066A,
+	SUN1999_BOARD_5016A,
+
+	//sun1999-multi I/O
+	SUN1999_BOARD_5069A,
+	SUN1999_BOARD_5079A,
+	SUN1999_BOARD_5099A,
+
+	//sun1999-parallel
+

can we have a bit more consistent formatting (eg. all those comments w/ same indention) ?

Quoted 2 lines
char/snx/snx_main.c +2 -0
+	SUN1999_BOARD_5008A,
+

<snip>

char/snx/snx_main.c line 84

Quoted 2 lines
char/snx/snx_main.c +2 -0
+static  struct pci_device_id	sunix_pci_board_id[] = {
+// golden-serial

can't this be const ?
(maybe even __initconst)

Quoted 78 lines
char/snx/snx_main.c +78 -0
+	{VENID_GOLDEN,	DEVID_G_SERIAL,	SUBVENID_GOLDEN,
+		SUBDEVID_TEST,	0,	0,	GOLDEN_BOARD_TEST},
+
+// matrix-serial
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1002,	0,	0,	MATRIX_BOARD_P1002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1004,	0,	0,	MATRIX_BOARD_P1004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1008,	0,	0,	MATRIX_BOARD_P1008},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P1016,	0,	0,	MATRIX_BOARD_P1016},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2002,	0,	0,	MATRIX_BOARD_P2002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2004,	0,	0,	MATRIX_BOARD_P2004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P2008,	0,	0,	MATRIX_BOARD_P2008},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3002,	0,	0,	MATRIX_BOARD_P3002},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3004,	0,	0,	MATRIX_BOARD_P3004},
+	{VENID_MATRIX,	DEVID_M_SERIAL,	SUBVENID_MATRIX,
+		SUBDEVID_P3008,	0,	0,	MATRIX_BOARD_P3008},
+
+	// sun1999-serial RS232
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5027A,	0,	0,	SUN1999_BOARD_5027A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5037A,	0,	0,	SUN1999_BOARD_5037A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5056A,	0,	0,	SUN1999_BOARD_5056A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5066A,	0,	0,	SUN1999_BOARD_5066A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5016A,	0,	0,	SUN1999_BOARD_5016A},
+
+	// sun1999-multi I/O
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5069A,	0,	0,	SUN1999_BOARD_5069A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5079A,	0,	0,	SUN1999_BOARD_5079A},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_5099A,	0,	0,	SUN1999_BOARD_5099A},
+
+	// sun1999-parallel
+	{VENID_SUN1999,	DEVID_S_PARALL,	SUBVENID_SUN1999,
+		SUBDEVID_5008A,	0,	0,	SUN1999_BOARD_5008A},
+
+	// sun1999-serial RS422/485
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2102,	0,	0,	SUN1999_BOARD_P2102},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2104,	0,	0,	SUN1999_BOARD_P2104},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2108,	0,	0,	SUN1999_BOARD_P2108},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P2116,	0,	0,	SUN1999_BOARD_P2116},
+
+	// sun1999 3_in_1
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P3104,	0,	0,	SUN1999_BOARD_P3104},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_P3108,	0,	0,	SUN1999_BOARD_P3108},
+
+	//cash drawer card  2S
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_CASH_2S, 0,	0,	SUN1999_BOARD_CASH_2S},
+
+	//cash drawer card  4S
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_CASH_4S, 0,	0,	SUN1999_BOARD_CASH_4S},
+
+	//DIO
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO0802, 0,	0,	SUN1999_BOARD_DIO0802},
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO1604, 0,	0,	SUN1999_BOARD_DIO1604},

char/snx/snx_main.c line 172

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	{VENID_SUN1999,	DEVID_S_SERIAL,	SUBVENID_SUN1999,
+		SUBDEVID_DIO3204, 0,	0,	SUN1999_BOARD_DIO3204},
+
+	{0}
+};
+MODULE_DEVICE_TABLE(pci, sunix_pci_board_id);
+
+struct sunix_board			sunix_board_table[SNX_BOARDS_MAX];
+struct sunix_ser_port	    sunix_ser_table[SNX_SER_TOTAL_MAX + 1];
+struct sunix_par_port	    sunix_par_table[SNX_PAR_TOTAL_MAX];

I'd really prefer separate port types - that go into separate subsystems, in separate modules (those can share code in a common module, if desired)

Oh, is there really a hard restriction on the max number of boards ?

And do we really need a global list of them ? (instead of just having all per-board / per-port data in a per-board / per-port driver instance)

char/snx/snx_main.c line 184

Quoted 11 lines
char/snx/snx_main.c +11 -0
+static int snx_ser_port_total_cnt;
+static int snx_par_port_total_cnt;
+
+int snx_board_count;
+
+static struct snx_ser_driver	sunix_ser_reg = {
+	.dev_name = "ttySNX",
+	.major = 0,
+	.minor = 0,
+	.nr = (SNX_SER_TOTAL_MAX + 1),
+};

can't this be const ?

Quoted 7 lines
char/snx/snx_main.c +7 -0
+static irqreturn_t sunix_interrupt(int irq, void *dev_id)
+{
+	struct sunix_ser_port *sp = NULL;
+	struct sunix_par_port *pp = NULL;
+	struct sunix_board *sb = NULL;
+	int i;
+	int status = 0;

char/snx/snx_main.c line 202

Quoted 10 lines
char/snx/snx_main.c +10 -0
+
+	int handled = IRQ_NONE;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+
+		if (dev_id == &(sunix_board_table[i])) {
+			sb = dev_id;
+			break;
+		}
+	}

maybe put this index into the board data, so the loop on the global array isn't needed ?

<snip>

char/snx/snx_main.c line 217

Quoted 2 lines
char/snx/snx_main.c +2 -0
+	if ((sb->ser_port > 0) && (sb->ser_isr != NULL)) {
+		sp = &sunix_ser_table[sb->ser_port_index];

maybe put this pointer struct sunix_board so the lookup in the global array isn't necessary ?

<snip>

Quoted 5 lines
char/snx/snx_main.c +5 -0
+	if ((sb->par_port > 0) && (sb->par_isr != NULL)) {
+		pp = &sunix_par_table[sb->par_port_index];
+
+		if (!pp)
+			status = 1;

char/snx/snx_main.c line 239

Quoted 10 lines
char/snx/snx_main.c +10 -0
+
+		status = sb->par_isr(sb, pp);
+	}
+
+	if (status != 0)
+		return handled;
+
+	handled = IRQ_HANDLED;
+	return handled;
+}

btw: is there only one irq per board or one per port ?

char/snx/snx_main.c line 243

Quoted 3 lines
char/snx/snx_main.c +3 -0
+static int snx_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+	return 0;

shouldn't probe check whether the device is actually there and then do the initialization ?

<snip>

char/snx/snx_main.c line 279

Quoted 12 lines
char/snx/snx_main.c +12 -0
+static int snx_resume_port_termios(struct snx_ser_info *info)
+{
+	struct snx_ser_state *state = NULL;
+	struct tty_struct *tty = info->tty;
+
+	state = tty->driver_data;
+	snx_set_port_termios(state);
+
+	return 0;
+}
+
+

do we need multiple newlines here ?

<snip>

char/snx/snx_main.c line 301

Quoted 11 lines
char/snx/snx_main.c +11 -0
+static int snx_resume_one(struct pci_dev *pdev)
+{
+	struct sunix_board *sb = pci_get_drvdata(pdev);
+	struct sunix_ser_port *sp = NULL;
+	int j;
+
+	if (sb == NULL)
+		return 0;
+
+	for (j = 0; j < sb->ser_port; j++) {
+		sp = &sunix_ser_table[j];

can't the pointers to the serial ports be in struct sunix_board instead of an global array ?

Quoted 21 lines
char/snx/snx_main.c +21 -0
+static int sunix_pci_board_probe(void)
+{
+	struct sunix_board *sb;
+	struct pci_dev *pdev = NULL;
+	struct pci_dev *pdev_array[4] = {NULL, NULL, NULL, NULL};
+
+	int sunix_pci_board_id_cnt;
+	int tablecnt;
+	int boardcnt;
+	int i;
+	unsigned short int sub_device_id;
+	unsigned short int device_part_number;
+	unsigned int bar3_base_add;
+
+	int status;
+	unsigned int bar3_Byte5;
+	unsigned int bar3_Byte6;
+	unsigned int bar3_Byte7;
+	unsigned int oem_id;
+	unsigned char uart_type;
+	unsigned char gpio_type;

char/snx/snx_main.c line 344

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	unsigned char gpio_card_type;
+	int gpio_ch_cnt;
+
+	// clear and init some variable
+	memset(sunix_board_table, 0, SNX_BOARDS_MAX *
+	sizeof(struct sunix_board));
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sunix_board_table[i].board_enum = -1;
+		sunix_board_table[i].board_number = -1;

if it already is a global, why not statically initialized ?

Quoted 60 lines
char/snx/snx_main.c +60 -0
+	}
+
+	sunix_pci_board_id_cnt =
+	(sizeof(sunix_pci_board_id) / sizeof(sunix_pci_board_id[0])) - 1;
+
+	// search matrix serial board
+	pdev = NULL;
+	tablecnt = 0;
+	boardcnt = 0;
+	sub_device_id = 0;
+	status = 0;
+
+	while (tablecnt < sunix_pci_board_id_cnt) {
+
+		pdev = pci_get_device(VENID_MATRIX, DEVID_M_SERIAL, pdev);
+
+		if (pdev == NULL) {
+			tablecnt++;
+			continue;
+		}
+
+		if ((tablecnt > 0) && ((pdev == pdev_array[0]) ||
+		(pdev == pdev_array[1]) ||
+		(pdev == pdev_array[2]) ||
+		(pdev == pdev_array[3]))) {
+			continue;
+		}
+
+		pci_read_config_word(pdev, 0x2e, &sub_device_id);
+
+		if (sub_device_id == 0) {
+			status = -EIO;
+			return status;
+		}
+
+		if (sub_device_id != sunix_pci_board_id[tablecnt].subdevice)
+			continue;
+		if (pdev == NULL) {
+			pr_err("SNX Error: PCI device object is NULL !\n");
+			status = -EIO;
+			return status;
+
+		} else {
+
+			status = pci_enable_device(pdev);
+
+			if (status != 0) {
+				pr_err("SNX Error: SUNIX Board Enable Fail !\n\n");
+				status = -ENXIO;
+				return status;
+			}
+		}
+
+		boardcnt++;
+		if (boardcnt > SNX_BOARDS_MAX) {
+			pr_err("SNX Error: Support Four Boards In Maximum !\n");
+			status = -ENOSPC;
+			return status;
+		}
+

char/snx/snx_main.c line 414

Quoted 10 lines
char/snx/snx_main.c +10 -0
+		sb = &sunix_board_table[boardcnt-1];
+		pdev_array[boardcnt-1] = pdev;
+		sb->pdev = pdev;
+		sb->bus_number = pdev->bus->number;
+		sb->dev_number = PCI_SLOT(pdev->devfn);
+		sb->board_enum = (int)sunix_pci_board_id[tablecnt].driver_data;
+		sb->pb_info = snx_pci_board_conf[sb->board_enum];
+		sb->board_flag = sb->pb_info.board_flag;
+		sb->board_number = boardcnt - 1;
+	}

Why not doing this in snx_pci_probe() on per-board basis, and let device registration be done by pci subsystem ?

Quoted 5 lines
char/snx/snx_main.c +5 -0
+	// search sun1999 muti I/O board
+	pdev = NULL;
+	tablecnt = 0;
+	sub_device_id = 0;
+	status = 0;

char/snx/snx_main.c line 430

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	device_part_number = 0;
+	bar3_base_add = 0;
+	bar3_Byte5 = 0;
+	bar3_Byte6 = 0;
+	bar3_Byte7 = 0;
+	oem_id = 0;
+	uart_type = 0;
+	gpio_type = 0;
+	gpio_card_type = 0;
+	gpio_ch_cnt = 0;

completely different boards handled by one big driver ? why not splitting it up into multiple drivers ?

<snip>

char/snx/snx_main.c line 494

Quoted 9 lines
char/snx/snx_main.c +9 -0
+		if ((gpio_ch_cnt == 0x00) && (gpio_card_type == 0x01))
+			gpio_ch_cnt = 6;
+		else if ((gpio_ch_cnt == 0x00) && (gpio_card_type == 0x02))
+			gpio_ch_cnt = 8;
+		else if (gpio_ch_cnt == 0x01)
+			gpio_ch_cnt = 16;
+		else if (gpio_ch_cnt == 0x02)
+			gpio_ch_cnt = 32;
+

gpio devices have their own subsystem -> therefore: write a separate gpio driver for that.

<snip>

Quoted 17 lines
char/snx/snx_main.c +17 -0
+static int sunix_get_pci_board_conf(void)
+{
+	struct sunix_board *sb = NULL;
+	struct pci_dev *pdev = NULL;
+	int status = 0;
+	int i;
+	int j;
+
+	for (i = 0; i < SNX_BOARDS_MAX; i++) {
+		sb = &sunix_board_table[i];
+
+		if (sb->board_enum > 0) {
+			pdev = sb->pdev;
+			sb->ports = sb->pb_info.num_serport +
+				sb->pb_info.num_parport;
+			sb->ser_port = sb->pb_info.num_serport;
+			sb->par_port = sb->pb_info.num_parport;

char/snx/snx_main.c line 662

Quoted 10 lines
char/snx/snx_main.c +10 -0
+			snx_ser_port_total_cnt = snx_ser_port_total_cnt +
+				sb->ser_port;
+			snx_par_port_total_cnt = snx_par_port_total_cnt +
+				sb->par_port;
+
+			if (snx_ser_port_total_cnt > SNX_SER_TOTAL_MAX) {
+				pr_err("SNX Error: Too much serial port\n");
+				status = -EIO;
+				return status;
+			}

why that limitation ? why does this have to be a global array at all ?

Quoted 8 lines
char/snx/snx_main.c +8 -0
+static int sunix_assign_resource(void)
+{
+	struct sunix_board *sb = NULL;
+	struct sunix_ser_port *sp = NULL;
+	struct sunix_par_port *pp = NULL;
+
+	int status = 0;
+	int i;

char/snx/snx_main.c line 709

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	int j;
+	int k;
+	int ser_n;
+	int ser_port_index = 0;
+
+	memset(sunix_ser_table, 0, (SNX_SER_TOTAL_MAX + 1) *
+	sizeof(struct sunix_ser_port));
+
+	memset(sunix_par_table, 0, (SNX_PAR_TOTAL_MAX) *
+	sizeof(struct sunix_par_port));

why this w/ global variables, instead of on per-device basis on device- private data ?

<snip>

char/snx/snx_main.c line 806

Quoted 2 lines
char/snx/snx_main.c +2 -0
+static int sunix_ser_port_table_init(void)
+{

same here.

<snip>

char/snx/snx_main.c line 930

Quoted 2 lines
char/snx/snx_main.c +2 -0
+static int sunix_par_port_table_init(void)
+{

same here

char/snx/snx_main.c

Quoted 2 lines
char/snx/snx_main.c +2 -0
+int sunix_register_irq(void)
+{

same here.

<snip>

Quoted 47 lines
char/snx/snx_main.c +47 -0
+static int __init snx_init(void)
+{
+	int status = 0;
+
+	pr_err("SNX Info : Loading SUNIX Multi-I/O Board Driver Module\n");
+
+	snx_ser_port_total_cnt = snx_par_port_total_cnt = 0;
+
+	status = sunix_pci_board_probe();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_get_pci_board_conf();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_assign_resource();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_ser_port_table_init();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_par_port_table_init();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_register_irq();
+	if (status != 0)
+		goto step1_fail;
+
+	status = sunix_ser_register_driver(&sunix_ser_reg);
+	if (status != 0)
+		goto step2_fail;
+
+	status = sunix_ser_register_ports(&sunix_ser_reg);
+	if (status != 0)
+		goto step3_fail;
+
+	status = pci_register_driver(&snx_pci_driver);
+	if (status != 0)
+		goto step7_fail;
+
+	if (snx_par_port_total_cnt > 0) {
+		status = sunix_par_parport_init();
+		if (status != 0)

char/snx/snx_main.c line 1150

Quoted 10 lines
char/snx/snx_main.c +10 -0
+			goto step4_fail;
+
+		status = sunix_par_ppdev_init();
+		if (status != 0)
+			goto step5_fail;
+
+		status = sunix_par_lp_init();
+		if (status != 0)
+			goto step6_fail;
+	}

why not doing those initializations on per-device basis, in the corresponding probe() function ?

Quoted 16 lines
char/snx/snx_main.c +16 -0
+step7_fail:
+
+		pci_unregister_driver(&snx_pci_driver);
+step6_fail:
+
+		sunix_par_ppdev_exit();
+
+
+step5_fail:
+
+		sunix_par_parport_exit();
+
+
+step4_fail:
+
+		sunix_ser_unregister_ports(&sunix_ser_reg);

char/snx/snx_main.c line 1185

Quoted 10 lines
char/snx/snx_main.c +10 -0
+	}
+
+step3_fail:
+
+	sunix_ser_unregister_driver(&sunix_ser_reg);
+
+
+step2_fail:
+
+	sunix_release_irq();

use devm_*() functions, which automatically releases resource when a device is deleted, so explicit release isn't needed anymore.

char/snx/snx_main.c line 1206

Quoted 7 lines
char/snx/snx_main.c +7 -0
+static void __exit snx_exit(void)
+{
+	if (snx_par_port_total_cnt > 0) {
+		sunix_par_lp_exit();
+		sunix_par_ppdev_exit();
+		sunix_par_parport_exit();
+	}

Let the cleanup be done by the individual driver's release functions, and down forget to set .owner correctly - that way, the kernel won't even allow unloading the module, as long as it's in use. then, you'd probably don't need much more than just removing unregistering the drivers here.

char/snx/snx_serial.c line 5

Quoted 11 lines
char/snx/snx_serial.c new file +5 -0
new file mode 100644
index 00000000..fdac4c90
@@ -0,0 +1,4263 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "snx_common.h"
+#include "driver_extd.h"
+
+#define SNX_ioctl_DBG	0

what exactly is that ioctl for ?
or more to the point: why are you introducing a driver specific iotctl ?

char/snx/snx_serial.c line 14

Quoted 9 lines
char/snx/snx_serial.c new file +9 -0
+#define	EEPROM_ACCESS_DELAY_COUNT			100000
+
+static DEFINE_SEMAPHORE(ser_port_sem);
+
+#define SNX_HIGH_BITS_OFFSET	((sizeof(long)-sizeof(int))*8)
+#define sunix_ser_users(state) \
+((state)->count + ((state)->info ? (state)->info->blocked_open : 0))
+
+static struct tty_port snx_service_port;

why has this to be global, instead of in per-device private data ?

<snip>

char/snx/snx_serial.c line 16

Quoted 2 lines
char/snx/snx_serial.c new file +2 -0
+static _INLINE_ void snx_ser_handle_cts_change(
+	struct snx_ser_port *, unsigned int);

Where's this strange "_INLINE_" coming from ?

<snip>

char/snx/snx_serial.c line 79

Quoted 2 lines
char/snx/snx_serial.c new file +2 -0
+//extern void		snx_ser_change_speed(
+	//struct snx_ser_state *state, struct SNXTERMIOS *old_termios);

please no dead/commented-out code.

char/snx/snx_serial.c line 122

Quoted 2 lines
char/snx/snx_serial.c new file +2 -0
+//extern int	sunix_ser_interrupt(
+	//struct sunix_board *, struct sunix_ser_port *first_sp);

same here

char/snx/snx_serial.c line 139

Quoted 4 lines
char/snx/snx_serial.c new file +4 -0
+//extern int      sunix_ser_register_ports(struct snx_ser_driver *drv);
+//extern void     sunix_ser_unregister_ports(struct snx_ser_driver *drv);
+//extern int      sunix_ser_register_driver(struct snx_ser_driver *drv);
+//extern void     sunix_ser_unregister_driver(struct snx_ser_driver *drv);

same here

char/snx/snx_serial.c line 164

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+static unsigned char READ_INTERRUPT_VECTOR_BYTE(struct sunix_ser_port *sp)

i'd recommend no caps in function names.

Quoted 4 lines
char/snx/snx_serial.c new file +4 -0
+		if (var == 0x04) {
+			vet4 = inb(local_vector + 0xb0);
+			vet4 <<= 12;
+		}

char/snx/snx_serial.c line 232

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+
+		data = (vet1 | vet2 | vet3 | vet4);
+
+		return data;
+	}
+	return 0;
+}
+
+
+

remove excess newlines

<snip>

char/snx/snx_serial.c line 349

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+static int EEPROMWriteData(int targetConfigAddress, int address, int data)

why do you use int instead of void* for addresses ? oh, and shouldn't it be __iomem ?

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+		Error = inb(targetConfigAddress + 0x08) & 0x04;

<snip>

char/snx/snx_serial.c line 483

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+static void snx_ser_start(struct tty_struct *tty)
+{
+	int line = SNX_SER_DEVNUM(tty);
+
+	if (line >= SNX_SER_TOTAL_MAX)
+		return;
+
+	//spin_lock_irqsave(&port->lock, flags);
+	__snx_ser_start(tty);
+	//spin_unlock_irqrestore(&port->lock, flags);

why are the spinlock calls commented out ?

<nsip>

char/snx/snx_serial.c

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	tasklet_kill(&info->tlet);

what exactly is the tasklet needed for ?

char/snx/snx_serial.c line 827

Quoted 2 lines
char/snx/snx_serial.c new file +2 -0
+	if (!capable(CAP_SYS_ADMIN)) {
+		retval = -EPERM;

why this explicit check for CAP_SYS_ADMIN ?

char/snx/snx_serial.c line 828

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+		if (change_irq ||

indention mismatch.

char/snx/snx_serial.c line 829

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+			change_port ||

why is userland allowed to change irq, port, etc, if that's probed from pci anyways ?

Quoted 13 lines
char/snx/snx_serial.c new file +13 -0
+			(new_serial.baud_base != port->uartclk / 16) ||
+			(close_delay != state->close_delay) ||
+			(closing_wait != state->closing_wait) ||
+			(new_serial.xmit_fifo_size != port->fifosize) ||
+			(((new_flags ^ old_flags) & ~SNX_UPF_USR_MASK) != 0)) {
+			goto exit;
+		}
+
+		port->flags = ((port->flags & ~SNX_UPF_USR_MASK) |
+			(new_flags & SNX_UPF_USR_MASK));
+		port->custom_divisor = new_serial.custom_divisor;
+		goto check_and_exit;
+	}

<snip>

Quoted 3 lines
char/snx/snx_serial.c new file +3 -0
+static int snx_ser_ioctl(struct tty_struct *tty,
+unsigned int cmd, unsigned long arg)
+{

<snip>

Quoted 14 lines
char/snx/snx_serial.c new file +14 -0
+	case TIOCSSERIAL:
+	{
+		if (line < SNX_SER_TOTAL_MAX) {
+			state->port->setserial_flag = SNX_SER_BAUD_SETSERIAL;
+			ret = snx_ser_set_info(state,
+				(struct serial_struct *)arg);
+		}
+		break;
+	}
+
+
+	case TCSETS:
+	{
+		if (line < SNX_SER_TOTAL_MAX) {

char/snx/snx_serial.c line 1191

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+			state->port->flags &= ~(SNX_UPF_SPD_HI |
+				SNX_UPF_SPD_VHI |
+				SNX_UPF_SPD_SHI |
+				SNX_UPF_SPD_WARP);
+			state->port->setserial_flag = SNX_SER_BAUD_NOTSETSER;
+			snx_ser_update_termios(state);
+		}
+		break;
+	}
+

Why do you need your own implementation of these tty ioctl()'s ? The tty subsystem handles them on its own (using the callbacks for hw- specific operations)

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_SER_DUMP_PORT_INFO:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_SER_DUMP_DRIVER_VER:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_COMM_GET_BOARD_CNT:

<snip>

char/snx/snx_serial.c line 1286

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_COMM_GET_BOARD_INFO:

is it really necessary to introduce your own driver-specific ioctl() ? why not putting these things into debugfs or sysfs ?

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_GET:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_SET:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_READ:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_WRITE:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_SET_DEFAULT:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_WRITE_DEFAULT:

<snip>

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_GET_INPUT_INVERT:

<snip>

char/snx/snx_serial.c line 1998

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_GPIO_SET_INPUT_INVERT:

gpio stuff clearly doesn't belong into tty ioctl()s.

that's what the gpio subsystem is there for - this provides the linux standard api for gpio access.

char/snx/snx_serial.c line 2062

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_UART_GET_TYPE:

what exactly is this for ?

<snip>

char/snx/snx_serial.c line 2122

Quoted 8 lines
char/snx/snx_serial.c new file +8 -0
+		} else {
+			//pr_err(CE_NOTE, "WARNING : incorrect port
+			//number (port = %d)!",gb.uart_num);
+			break;
+		}
+
+			switch (uart_type) {
+			case 0: // RS-232

yet another indention mismatch.
and please remove dead code.

<snip>

char/snx/snx_serial.c line 2167

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_UART_SET_TYPE: {

what is this for ?

<snip>

char/snx/snx_serial.c line 2436

Quoted 1 line
char/snx/snx_serial.c new file +1 -0
+	case SNX_UART_SET_ACS:

what is "ACS" ?

Quoted 5 lines
char/snx/snx_serial.c new file +5 -0
+	{
+		SNX_DRVR_UART_SET_ACS gb;
+		struct sunix_board	*sb = NULL;
+		int ACSstate = 0;
+		int targetConfigAddress = 0;

char/snx/snx_serial.c line 2451

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+
+		memset(&gb, 0, sizeof(SNX_DRVR_UART_SET_ACS));
+
+		if (copy_from_user(&gb, (void *)arg,
+			(sizeof(SNX_DRVR_UART_SET_ACS))))
+			ret = -EFAULT;
+		else
+			ret = 0;
+
+		sb = &sunix_board_table[gb.board_id - 1];

do we really need to access global variables here ?

<snip>

char/snx/snx_serial.c line 2494

Quoted 7 lines
char/snx/snx_serial.c new file +7 -0
+	if (tty->flags & (1 << TTY_IO_ERROR)) {
+		ret = -EIO;
+		goto out;
+	}
+
+		switch (cmd) {
+		case TIOCMIWAIT:

yet another idention mismatch.

<snip>

Quoted 6 lines
char/snx/snx_serial.c new file +6 -0
+	if (line < SNX_SER_TOTAL_MAX) {
+		down(&state->sem);
+
+			switch (cmd) {
+			case TIOCSERGETLSR:
+			ret = snx_ser_get_lsr_info(state, (unsigned int *)arg);

char/snx/snx_serial.c line 2526

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+			break;
+
+			default:
+			{
+				break;
+			}
+			}
+
+		up(&state->sem);
+	}

even more indention mismatches.

<snip>

Quoted 4 lines
char/snx/snx_serial.c new file +4 -0
+static void snx_ser_set_termios(struct tty_struct *tty,
+struct SNXTERMIOS *old_termios)
+{
+	struct snx_ser_state *state = NULL;

char/snx/snx_serial.c line 2756

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+	unsigned long flags;
+	unsigned int cflag = tty->termios.c_cflag;
+	int line = SNX_SER_DEVNUM(tty);
+
+	if (line >= SNX_SER_TOTAL_MAX)
+		return;
+
+	state = tty->driver_data;
+
+#define RELEVANT_IFLAG(iflag)	((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))

please no #define's in the middle of a function.

<snip>

char/snx/snx_serial.c line 2808

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+static void snx_ser_update_timeout(struct snx_ser_port *port,
+unsigned int cflag, unsigned int baud)
+{
+	unsigned int bits;
+
+	switch (cflag & CSIZE) {
+	case CS5:
+	bits = 7;
+	break;
+

more indention mismatches.

<snip>

Quoted 52 lines
char/snx/snx_serial.c new file +52 -0
+static int snx_ser_open(struct tty_struct *tty, struct file *filp)
+{
+	struct snx_ser_driver *drv =
+	(struct snx_ser_driver *)tty->driver->driver_state;
+
+	struct snx_ser_state *state = NULL;
+	struct tty_port *tport = NULL;
+
+	int retval = 0;
+	int line = SNX_SER_DEVNUM(tty);
+
+	if (line < SNX_SER_TOTAL_MAX) {
+		retval = -ENODEV;
+
+		if (line >= SNX_SER_TOTAL_MAX)
+			goto fail;
+
+		state = snx_ser_get(drv, line);
+
+		tport = &state->tport;
+
+		if (IS_ERR(state)) {
+			retval = PTR_ERR(state);
+			goto fail;
+		}
+
+		if (!state)
+			goto fail;
+
+
+		state->port->suspended = 1;
+		tty->driver_data = state;
+
+		tport->low_latency = (state->port->flags &
+		SNX_UPF_LOW_LATENCY) ? 1 : 0;
+
+		state->info->tty = tty;
+
+		tty_port_tty_set(tport, tty);
+
+		if (tty_hung_up_p(filp)) {
+			retval = -EAGAIN;
+			state->count--;
+			up(&state->sem);
+			goto fail;
+		}
+
+		retval = snx_ser_startup(state, 0);
+
+		if (retval == 0)
+			retval = snx_ser_block_til_ready(filp, state);
+

char/snx/snx_serial.c line 3053

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+		up(&state->sem);
+
+		if (retval == 0 && !(state->info->flags &
+			SNX_UIF_NORMAL_ACTIVE)) {
+			state->info->flags |= SNX_UIF_NORMAL_ACTIVE;
+
+			snx_ser_update_termios(state);
+		}
+
+		try_module_get(THIS_MODULE);

why this ?

<snip>

Quoted 13 lines
char/snx/snx_serial.c new file +13 -0
+extern int sunix_ser_register_driver(struct snx_ser_driver *drv)
+{
+	struct tty_driver *normal = NULL;
+	int i;
+	int ret = 0;
+
+	drv->state = kmalloc(sizeof(struct snx_ser_state) * drv->nr,
+		GFP_KERNEL);
+
+	ret = -ENOMEM;
+
+	if (!drv->state) {
+		pr_err("SNX Error: Allocate memory fail !\n\n");

char/snx/snx_serial.c line 3829

Quoted 10 lines
char/snx/snx_serial.c new file +10 -0
+		goto out;
+	}
+
+	memset(drv->state, 0, sizeof(struct snx_ser_state) * drv->nr);
+
+	for (i = 0; i < drv->nr; i++) {
+		struct snx_ser_state *state = drv->state + i;
+		struct tty_port *tport = &state->tport;
+
+		tty_port_init(tport);

does that really need to be globally in driver init, instead of in per port device->open (and use device's private data) ?

<snip>

Quoted 1 line

+extern void sunix_ser_unregister_ports(struct snx_ser_driver *drv)

why are these 'extern' ?!

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#2 lore raw
In reply to Morris Ku
Quoted 8 lines · Morris Ku

Driver for SUNIX Multi-I/O card.
Based on driver/char/serial.c by Linus Torvalds, Theodore Ts'o.

SUNIX serial card designed with SUNIX UART controller and compatible with 16C950 UART specification.

Hi Morris,

Thanks for your submission!

Enrico has already replied with the most important comments. Here are just some more high-level thoughts from me:

* We want the functionality to be present in subsystem specific standalone drivers, i.e. drivers/tty/serial/, drivers/parport/, drivers/gpio etc, as Enrico said

* It looks like you may need some glue logic to tie those together for the multi-I/O cards. This is a bit tricky but doable. Many other multi-I/O cards simply appear as a set of PCI functions that are connected to a single slot. This is the ideal case, as you can simply have one driver per function and they do not have to be linked together at all. If this is not possible, what you may need to do here is to have a small driver in drivers/mfd/ that handles the PCI function and creates platform_device instances that the individual drivers can bind to. Have a look at the various files in drivers/mfd that come with a 'struct pci_driver' instance to see what I mean.

* In any way, new drivers must probing according to our driver module if possible. This means you should have a 'pci_driver' structure listing the possible vendor/device ID combinations, and register that with 'module_pci_driver'. If you use mfd_cell to register sub-drivers, use 'module_platform_driver' to register the driver for those.

* do not implement your own ioctl handlers. If you need a custom interface for something hardware specific, split that out into a separate patch, so the base support can be reviewed independently.

* If the functions (in particular serial and parport) have a similar register layout to existing drivers, try to reuse the existing code and extend it, rather than duplicating the implementation.

Arnd

#3 lore raw

On 11.03.19 16:05, Arnd Bergmann wrote:

Hi,

Quoted 1 line

Enrico has already replied with the most important comments. Here> are just some more high-level thoughts from me:

I'll try to make things a bit more clear to newcomers:

Quoted 1 line

* We want the functionality to be present in subsystem specific> standalone drivers, i.e. drivers/tty/serial/, drivers/parport/,

drivers/gpio> etc, as Enrico said
That means we'd have completely separate drivers for the uart, parport, gpio, etc. These might share some common code (eg. some board config data which is used by all drivers), in a separate module.

Quoted 1 line

If this is not possible, what you may need to do here is to have a> small driver in drivers/mfd/ that handles the PCI function and

creates> platform_device instances that the individual drivers can
bind to.

'bind to' here means, the mfd driver will prepare the platform data (platform specific config struct) for the actual drivers and creates instances of them (see platform_device_register*() functions). The kernel will then load the corresponding modules (if not compiled-in) and calls these drivers with the platform data passed in to it's probe() callback.

An example could be my recent gpio-amd-fch and pcengines-apuv2 drivers. Here, the gpio-amd-fch drives the gpio's in the SoC, while the pcengines-apuv2 driver one is just binding drivers together - the actual config (register addressed, etc) is in the pcengines-apu2 driver:

See:
drivers/gpio/gpio-amd-fch.c
drivers/platform/x86/pcengines-apuv2.c

Maybe it would be a good start, spliiting out the gpio stuff into it's own driver. If the gpio subdevice can't be probed individually (eg. has it's own pci function), you could do it the way I did it w/ gpio-amd-fch and pcengines-apuv2.

Quoted 1 line

* do not implement your own ioctl handlers. If you need a custom> interface for something hardware specific, split that out into> a

separate patch, so the base support can be reviewed> independently.
You really should try to get around w/o adding any special ioctl()s or other userland interfaces. If you *really* believe you can't do that, please let's talk about this first. Such cases usually indicate a missing generic functionality in one of the subsystems.

Let me stress some very important point here (which also I frequently have to explain to my clients): in Linux, the kernel is *the* "hardware abstraction layer". Applications can just use generic APIs and never care about the actual underlying hardware - the kernel just presents everything by a uniform interface. That's why drivers really just dock into the corresponding subsystems and not introduce their own APIs.

There're only few exceptions from that, eg. DRI subsystem, because these devices tend to be so specific, that it just doesn't make sense to create a uniform interface (unless we'd want to move entire gallium into the kernel ;-)).

From what I can see in your patches, your cards provide pretty standard uart, parport, gpio.

Quoted 1 line

* If the functions (in particular serial and parport) have a similar> register layout to existing drivers, try to reuse the existing code>

and extend it, rather than duplicating the implementation.
Yes, please have a closer look into the corresponding subsystems, which generic drivers already exist, that might fit to your device.

For example, in gpio, we've got a generic driver for devices that map all the direction flags into one, and the line statii into another register (see bgpio_*() functions). We're yet lacking another generic one that puts everything for one line into one register (and then has an array of those) - that's still on my todo list.

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287