[PATCH 09/19] Char: moxa, centralize board readiness

2008-02-22 Thread Jiri Slaby
The only relevant sign of port being ready is its board->ready since now.
Remove all other flags for this purpose which are set almost on the same
place. Move ports inside the board to be sure that nobody will grab
reference to the port without being sure that it exists.

Signed-off-by: Jiri Slaby <[EMAIL PROTECTED]>
Tested-by: Oyvind Aabling <[EMAIL PROTECTED]>
---
 drivers/char/moxa.c |  232 +--
 1 files changed, 114 insertions(+), 118 deletions(-)

diff --git a/drivers/char/moxa.c b/drivers/char/moxa.c
index c440ec5..e2e8580 100644
--- a/drivers/char/moxa.c
+++ b/drivers/char/moxa.c
@@ -104,7 +104,7 @@ static struct moxa_board_conf {
int numPorts;
int busType;
 
-   int loadstat;
+   unsigned int ready;
 
struct moxa_port *ports;
 
@@ -142,7 +142,6 @@ struct moxa_port {
 
struct timer_list emptyTimer;
 
-   char chkPort;
char lineCtrl;
void __iomem *tableAddr;
char DCDState;
@@ -162,7 +161,6 @@ struct moxa_port {
 #define WAKEUP_CHARS   256
 
 static int ttymajor = MOXAMAJOR;
-static int moxaCard;
 /* Variables for insmod */
 #ifdef MODULE
 static unsigned long baseaddr[MAX_BOARDS];
@@ -218,7 +216,6 @@ static void moxa_receive_data(struct moxa_port *);
 static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
 static int MoxaDriverPoll(void);
 static int MoxaPortsOfCard(int);
-static int MoxaPortIsValid(int);
 static void MoxaPortEnable(struct moxa_port *);
 static void MoxaPortDisable(struct moxa_port *);
 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
@@ -263,7 +260,6 @@ static const struct tty_operations moxa_ops = {
 };
 
 static struct tty_driver *moxaDriver;
-static struct moxa_port moxa_ports[MAX_PORTS];
 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
 static DEFINE_SPINLOCK(moxa_lock);
 
@@ -480,7 +476,6 @@ static int moxa_real_load_code(struct moxa_board_conf *brd, 
const void *ptr,
if (readw(baseAddr + Magic_no) != Magic_code)
return -EIO;
}
-   moxaCard = 1;
brd->intNdx = baseAddr + IRQindex;
brd->intPend = baseAddr + IRQpending;
brd->intTable = baseAddr + IRQtable;
@@ -511,7 +506,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
port = brd->ports;
for (i = 0; i < brd->numPorts; i++, port++) {
port->board = brd;
-   port->chkPort = 1;
port->DCDState = 0;
port->tableAddr = baseAddr + Extern_table +
Extern_size * i;
@@ -530,7 +524,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
port = brd->ports;
for (i = 0; i < brd->numPorts; i++, port++) {
port->board = brd;
-   port->chkPort = 1;
port->DCDState = 0;
port->tableAddr = baseAddr + Extern_table +
Extern_size * i;
@@ -575,7 +568,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
}
break;
}
-   brd->loadstat = 1;
return 0;
 }
 
@@ -672,8 +664,29 @@ static int moxa_init_board(struct moxa_board_conf *brd, 
struct device *dev)
 {
const struct firmware *fw;
const char *file;
+   struct moxa_port *p;
+   unsigned int i;
int ret;
 
+   brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
+   GFP_KERNEL);
+   if (brd->ports == NULL) {
+   printk(KERN_ERR "cannot allocate memory for ports\n");
+   ret = -ENOMEM;
+   goto err;
+   }
+
+   for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
+   p->type = PORT_16550A;
+   p->close_delay = 5 * HZ / 10;
+   p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
+   init_waitqueue_head(>open_wait);
+   init_completion(>close_wait);
+
+   setup_timer(>emptyTimer, moxa_check_xmit_empty,
+   (unsigned long)p);
+   }
+
switch (brd->boardType) {
case MOXA_BOARD_C218_ISA:
case MOXA_BOARD_C218_PCI:
@@ -690,16 +703,38 @@ static int moxa_init_board(struct moxa_board_conf *brd, 
struct device *dev)
ret = request_firmware(, file, dev);
if (ret) {
printk(KERN_ERR "request_firmware failed\n");
-   goto end;
+   goto err_free;
}
 
ret = moxa_load_fw(brd, fw);
 
release_firmware(fw);
-end:
+
+   if (ret)
+   goto err_free;
+
+   brd->ready = 1;
+
+   return 0;
+err_free:
+   kfree(brd->ports);
+err:
return ret;
 }
 
+static void moxa_board_deinit(struct 

[PATCH 09/19] Char: moxa, centralize board readiness

2008-02-22 Thread Jiri Slaby
The only relevant sign of port being ready is its board-ready since now.
Remove all other flags for this purpose which are set almost on the same
place. Move ports inside the board to be sure that nobody will grab
reference to the port without being sure that it exists.

Signed-off-by: Jiri Slaby [EMAIL PROTECTED]
Tested-by: Oyvind Aabling [EMAIL PROTECTED]
---
 drivers/char/moxa.c |  232 +--
 1 files changed, 114 insertions(+), 118 deletions(-)

diff --git a/drivers/char/moxa.c b/drivers/char/moxa.c
index c440ec5..e2e8580 100644
--- a/drivers/char/moxa.c
+++ b/drivers/char/moxa.c
@@ -104,7 +104,7 @@ static struct moxa_board_conf {
int numPorts;
int busType;
 
-   int loadstat;
+   unsigned int ready;
 
struct moxa_port *ports;
 
@@ -142,7 +142,6 @@ struct moxa_port {
 
struct timer_list emptyTimer;
 
-   char chkPort;
char lineCtrl;
void __iomem *tableAddr;
char DCDState;
@@ -162,7 +161,6 @@ struct moxa_port {
 #define WAKEUP_CHARS   256
 
 static int ttymajor = MOXAMAJOR;
-static int moxaCard;
 /* Variables for insmod */
 #ifdef MODULE
 static unsigned long baseaddr[MAX_BOARDS];
@@ -218,7 +216,6 @@ static void moxa_receive_data(struct moxa_port *);
 static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
 static int MoxaDriverPoll(void);
 static int MoxaPortsOfCard(int);
-static int MoxaPortIsValid(int);
 static void MoxaPortEnable(struct moxa_port *);
 static void MoxaPortDisable(struct moxa_port *);
 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
@@ -263,7 +260,6 @@ static const struct tty_operations moxa_ops = {
 };
 
 static struct tty_driver *moxaDriver;
-static struct moxa_port moxa_ports[MAX_PORTS];
 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
 static DEFINE_SPINLOCK(moxa_lock);
 
@@ -480,7 +476,6 @@ static int moxa_real_load_code(struct moxa_board_conf *brd, 
const void *ptr,
if (readw(baseAddr + Magic_no) != Magic_code)
return -EIO;
}
-   moxaCard = 1;
brd-intNdx = baseAddr + IRQindex;
brd-intPend = baseAddr + IRQpending;
brd-intTable = baseAddr + IRQtable;
@@ -511,7 +506,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
port = brd-ports;
for (i = 0; i  brd-numPorts; i++, port++) {
port-board = brd;
-   port-chkPort = 1;
port-DCDState = 0;
port-tableAddr = baseAddr + Extern_table +
Extern_size * i;
@@ -530,7 +524,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
port = brd-ports;
for (i = 0; i  brd-numPorts; i++, port++) {
port-board = brd;
-   port-chkPort = 1;
port-DCDState = 0;
port-tableAddr = baseAddr + Extern_table +
Extern_size * i;
@@ -575,7 +568,6 @@ static int moxa_load_code(struct moxa_board_conf *brd, 
const void *ptr,
}
break;
}
-   brd-loadstat = 1;
return 0;
 }
 
@@ -672,8 +664,29 @@ static int moxa_init_board(struct moxa_board_conf *brd, 
struct device *dev)
 {
const struct firmware *fw;
const char *file;
+   struct moxa_port *p;
+   unsigned int i;
int ret;
 
+   brd-ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd-ports),
+   GFP_KERNEL);
+   if (brd-ports == NULL) {
+   printk(KERN_ERR cannot allocate memory for ports\n);
+   ret = -ENOMEM;
+   goto err;
+   }
+
+   for (i = 0, p = brd-ports; i  MAX_PORTS_PER_BOARD; i++, p++) {
+   p-type = PORT_16550A;
+   p-close_delay = 5 * HZ / 10;
+   p-cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
+   init_waitqueue_head(p-open_wait);
+   init_completion(p-close_wait);
+
+   setup_timer(p-emptyTimer, moxa_check_xmit_empty,
+   (unsigned long)p);
+   }
+
switch (brd-boardType) {
case MOXA_BOARD_C218_ISA:
case MOXA_BOARD_C218_PCI:
@@ -690,16 +703,38 @@ static int moxa_init_board(struct moxa_board_conf *brd, 
struct device *dev)
ret = request_firmware(fw, file, dev);
if (ret) {
printk(KERN_ERR request_firmware failed\n);
-   goto end;
+   goto err_free;
}
 
ret = moxa_load_fw(brd, fw);
 
release_firmware(fw);
-end:
+
+   if (ret)
+   goto err_free;
+
+   brd-ready = 1;
+
+   return 0;
+err_free:
+   kfree(brd-ports);
+err:
return ret;
 }
 
+static void moxa_board_deinit(struct moxa_board_conf *brd)
+{
+   unsigned