* [PATCH] max3100 driver
@ 2007-12-05 10:26 chripell
2007-12-12 9:49 ` Andrew Morton
0 siblings, 1 reply; 21+ messages in thread
From: chripell @ 2007-12-05 10:26 UTC (permalink / raw)
To: linux-kernel; +Cc: Christian Pellegrin
This patch adds support for the MAX3100 SPI UART.
Generated on 20071205 against v2.6.23
Signed-off-by: Christian Pellegrin <chripell@fsfe.org>
---
drivers/serial/Kconfig | 7 +
drivers/serial/Makefile | 1 +
drivers/serial/max3100.c | 960 ++++++++++++++++++++++++++++++++++++++++
include/linux/serial_max3100.h | 25 +
4 files changed, 993 insertions(+), 0 deletions(-)
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 81b52b7..4e7111b 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -461,6 +461,13 @@ config SERIAL_S3C2410_CONSOLE
your boot loader about how to pass options to the kernel at
boot time.)
+config SERIAL_MAX3100
+ tristate "MAX3100 support"
+ depends on SPI
+ help
+ MAX3100 chip support
+
+
config SERIAL_DZ
bool "DECstation DZ serial driver"
depends on MACH_DECSTATION && 32BIT
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index af6377d..9f67e52 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_SERIAL_PNX8XXX) += pnx8xxx_uart.o
obj-$(CONFIG_SERIAL_SA1100) += sa1100.o
obj-$(CONFIG_SERIAL_BFIN) += bfin_5xx.o
obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
+obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
obj-$(CONFIG_SERIAL_SUNHV) += sunhv.o
obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
new file mode 100644
index 0000000..dd48258
--- /dev/null
+++ b/drivers/serial/max3100.c
@@ -0,0 +1,960 @@
+
+/*
+ *
+ * Copyright (C) 2007 Christian Pellegrin <chripell@evolware.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * version 0.1 31/5/2006 first public release
+ * version 0.2 11/11/2007 ported to kernel 2.6.23
+ *
+ *
+ *
+ * Notes: the MAX3100 doesn't provide an interrupt on CTS
+ * so we have to use polling for flow control. TX empty
+ * IRQ is unusable, since writing conf clears FIFO buffer.
+ *
+ * Example platform data:
+
+static struct plat_max3100 max3100_plat_data = {
+ .loopback = 0,
+ .crystal = 0,
+ .only_edge_irq = 0,
+};
+
+static struct spi_board_info spi_board_info[] = {
+ {
+ .modalias = "max3100",
+ .platform_data = &max3100_plat_data,
+ .irq = IRQ_EINT12,
+ .max_speed_hz = 5*1000*1000,
+ .chip_select = 0,
+ },
+};
+
+ */
+
+#include <linux/bitops.h>
+#include <linux/console.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/fcntl.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/keyboard.h>
+#include <linux/major.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/sched.h>
+#include <linux/serial_core.h>
+#include <linux/signal.h>
+#include <linux/spi/spi.h>
+#include <linux/string.h>
+#include <linux/timer.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
+#include <linux/workqueue.h>
+
+#include <asm/delay.h>
+#include <asm/io.h>
+#include <asm/irq.h>
+#include <asm/semaphore.h>
+#include <asm/system.h>
+#include <asm/uaccess.h>
+
+#include <linux/serial_max3100.h>
+
+#define MAX3100_C (1<<14)
+#define MAX3100_D (0<<14)
+#define MAX3100_W (1<<15)
+#define MAX3100_RX (0<<15)
+
+#define MAX3100_WC (MAX3100_W | MAX3100_C)
+#define MAX3100_RC (MAX3100_RX| MAX3100_C)
+#define MAX3100_WD (MAX3100_W | MAX3100_D)
+#define MAX3100_RD (MAX3100_RX| MAX3100_D)
+#define MAX3100_CMD (3 << 14)
+
+#define MAX3100_T (1<<14)
+#define MAX3100_R (1<<15)
+
+#define MAX3100_FEN (1<<13)
+#define MAX3100_SHDN (1<<12)
+#define MAX3100_TM (1<<11)
+#define MAX3100_RM (1<<10)
+#define MAX3100_PM (1<<9)
+#define MAX3100_RAM (1<<8)
+#define MAX3100_IR (1<<7)
+#define MAX3100_ST (1<<6)
+#define MAX3100_PE (1<<5)
+#define MAX3100_L (1<<4)
+#define MAX3100_BAUD (0xf)
+
+#define MAX3100_TE (1<<10)
+#define MAX3100_RAFE (1<<10)
+#define MAX3100_RTS (1<<9)
+#define MAX3100_CTS (1<<9)
+#define MAX3100_PT (1<<8)
+#define MAX3100_DATA (0xff)
+
+#define MAX3100_RT (MAX3100_R | MAX3100_T)
+#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
+
+struct max3100_port_s {
+ struct uart_port port;
+ struct spi_device *spi;
+ struct tty_struct *tty;
+
+ struct semaphore spi_txrx; /* protects access to the hw */
+
+ int rts; /* rts status, can be MAX3100_RTS or 0 */
+ int conf; /* configuration for the MAX31000
+ * (bits 0-7, bits 8-11 are irqs) */
+ int last_cts_rx; /* last CTS received for flow ctrl */
+
+ int tx_buf_cur; /* current char to tx */
+ int tx_buf_tot; /* current number of chars in tx buf */
+ unsigned char *tx_buf;
+ spinlock_t tx_buf_lock; /* shared tx buffer spinlock (no sem
+ * since write may sleep) */
+
+ int tx_stopped; /* when we should not send chars */
+ int parity; /* keeps track if we should work parity */
+#define MAX3100_PARITY_ON 1
+#define MAX3100_PARITY_ODD 2
+#define MAX3100_7BIT 4
+
+ int irq; /* irq assigned to the max3100 */
+
+ int minor; /* minor number */
+ int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
+ int loopback; /* 1 if we are in loopback mode */
+ int only_edge_irq; /* 1 if we have only edge irqs (like PXA) */
+
+ int ref_count; /* how many users */
+ struct semaphore sem; /* protects us during open/close */
+
+ struct workqueue_struct *workqueue;
+ /* for handling irqs: need workqueue since we do spi_sync */
+ struct work_struct work;
+ int force_end_work;
+ /* set to 1 to make the worknadler exit as soon as possible */
+
+ int irq_status;
+ spinlock_t irq_lock;
+ /* these are for IRQ on/off counting */
+};
+
+static struct tty_driver *serial_driver = NULL;
+/* global since we do register the driver only once */
+
+#define MAX_MAX3100 4 /* 4 MAX3100s should be enough for everyone */
+static struct max3100_port_s * max3100s[MAX_MAX3100]; /* the chips */
+
+#define MAX3100_TX_BUF_N 16
+/* our buffer for sendind chars */
+
+#define MAX3100_FLIP_EVERY 8
+/* how many chars we wait befor flipping tty buffer */
+
+static void irq_state(struct max3100_port_s *s, int on) {
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->irq_lock, flags);
+ if (s->irq_status != on) {
+ if (on) {
+ enable_irq(s->irq);
+ }
+ else {
+ disable_irq(s->irq);
+ }
+ s->irq_status = on;
+ }
+ spin_unlock_irqrestore(&s->irq_lock, flags);
+}
+
+
+static int max3100_do_parity(struct max3100_port_s *s, u16 c)
+{
+ int parity;
+ int i,n;
+
+ if (s->parity & MAX3100_PARITY_ODD)
+ parity = 0;
+ else
+ parity = 1;
+
+ if (s->parity & MAX3100_7BIT)
+ n = 7;
+ else
+ n = 8;
+
+ for(i=0;i<n;i++) {
+ parity = parity ^ ( (c>>i) & 1);
+ }
+ return parity;
+}
+
+static inline int max3100_check_parity(struct max3100_port_s *s, u16 c)
+{
+ return max3100_do_parity(s, c) == ( (c>>9) & 1);
+}
+
+static inline void max3100_calc_parity(struct max3100_port_s *s, u16 *c)
+{
+ *c &= ~MAX3100_PT;
+ *c |= max3100_do_parity(s, *c)<<8;
+}
+
+
+static void max3100_work(struct work_struct *w);
+
+static inline void max3100_dowork(struct max3100_port_s *s)
+{
+ if (!s->force_end_work) {
+ PREPARE_WORK(&s->work, max3100_work);
+ queue_work(s->workqueue, &s->work);
+ }
+}
+
+static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx, int push)
+{
+ /* note: we suppose that the T bit fron conf_status is in sync
+ * with the hw one, so all the communication must go through
+ * this function */
+ struct spi_message message;
+ struct spi_transfer x;
+ int status, flag;
+ u16 etx,erx;
+ int got_char = 0;
+
+ down(&s->spi_txrx);
+
+ if (s->loopback) {
+ if ((tx & MAX3100_CMD) == MAX3100_RC)
+ tx |= 1;
+ }
+ etx = htons(tx);
+ spi_message_init(&message);
+ memset(&x, 0, sizeof(x));
+ x.tx_buf = &etx;
+ x.rx_buf = &erx;
+ x.len = 2;
+ spi_message_add_tail(&x, &message);
+ status = spi_sync(s->spi, &message);
+ if (status) {
+ dev_warn(&s->spi->dev, "error while calling spi_sync\n");
+ return 0;
+ }
+ *rx = ntohs(erx);
+ //printk("%04x - %04x\n", tx, *rx);
+
+ if ((tx & MAX3100_CMD) == MAX3100_RD ||
+ (tx & MAX3100_CMD) == MAX3100_WD) {
+ s->last_cts_rx = *rx;
+ }
+
+ if (*rx & MAX3100_R &&
+ ((tx & MAX3100_CMD) == MAX3100_RD ||
+ (tx & MAX3100_CMD) == MAX3100_WD)) {
+ unsigned int flags;
+
+ got_char = 1;
+ if (tty_buffer_request_room(s->tty, 1) == 0) {
+ dev_warn(&s->spi->dev, "no room in tty buffer\n");
+ tty_schedule_flip(s->tty);
+ }
+
+ flag = TTY_NORMAL;
+ if (*rx & MAX3100_RAFE) {
+ s->port.icount.frame++;
+ flags = TTY_FRAME;
+ }
+ if (s->parity & MAX3100_PARITY_ON && max3100_check_parity(s, *rx)) {
+ s->port.icount.parity++;
+ flags = TTY_PARITY;
+ }
+ tty_insert_flip_char(s->tty, *rx & MAX3100_DATA, flag);
+ }
+
+ up(&s->spi_txrx);
+
+ if (push && got_char) {
+ tty_schedule_flip(s->tty);
+ }
+ return got_char;
+}
+
+
+static void max3100_send_conf(struct max3100_port_s *s)
+{
+ u16 tx=0,rx;
+
+ tx = MAX3100_WC | (s->conf & 0x0fff);
+ max3100_sr(s, tx, &rx, 1);
+}
+
+static inline int max3100_ok_to_send(struct max3100_port_s *s)
+{
+ if (C_CRTSCTS(s->tty)) {
+ if ((s->last_cts_rx & MAX3100_CTS) == 0)
+ return 0;
+ }
+ return ! s->tx_stopped;
+}
+
+static void max3100_work(struct work_struct *w)
+{
+ struct max3100_port_s *s = container_of(w, struct max3100_port_s, work);
+ u16 tx,rx;
+ unsigned long flags;
+ int nchars; /* number of char waiting in the send buf */
+ int rxchars = 0; /* chars received */
+
+ do {
+ tx = MAX3100_RD;
+ rxchars += max3100_sr(s, tx, &rx, 0);
+ /* rx a char so we get MAX3100_T and CTS current */
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ nchars = s->tx_buf_tot - s->tx_buf_cur;
+ tx = MAX3100_WD | s->rts;
+ tx |= s->tx_buf[s->tx_buf_cur];
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+
+ if ((rx & MAX3100_T) && /* tx buffer empty*/
+ max3100_ok_to_send(s) && /* fw ctrl decision */
+ nchars > 0 && /* more to send */
+ ! s->force_end_work) {
+ if (s->parity & MAX3100_PARITY_ON)
+ max3100_calc_parity(s, &tx);
+ rxchars += max3100_sr(s, tx, &rx, 0);
+ nchars -= 1;
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ s->tx_buf_cur += 1;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ if (nchars == 0) {
+ tty_wakeup(s->tty);
+ }
+ }
+ if (rxchars > MAX3100_FLIP_EVERY) {
+ tty_schedule_flip(s->tty);
+ rxchars = 0;
+ }
+ }
+ while ((nchars > 0 || /* more to send */
+ rx & MAX3100_R) && /* try to empy all the RX buffer */
+ ! s->force_end_work);
+ if (rxchars) {
+ tty_schedule_flip(s->tty);
+ }
+ if (! s->only_edge_irq && ! s->force_end_work)
+ irq_state(s, 1);
+}
+
+static irqreturn_t max3100_irq(int irqno, void *dev_id)
+{
+ struct max3100_port_s *s = dev_id;
+
+ if (! s->only_edge_irq)
+ irq_state(s, 0); /* avoid irq storm */
+ max3100_dowork(s);
+ return IRQ_HANDLED;
+}
+
+static void rs_stop(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+
+ s->tx_stopped = 1;
+}
+
+static void rs_start(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+
+ s->tx_stopped = 0;
+}
+
+static void rs_flush_chars(struct tty_struct *tty)
+{
+}
+
+static void rs_waint_until_sent(struct tty_struct *tty, int timeout)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ unsigned long jstop;
+
+ jstop = jiffies + timeout;
+ do {
+ if (s->tx_buf_tot == 0 || s->tx_buf_tot == s->tx_buf_cur)
+ return;
+ schedule();
+ } while (!timeout || time_before(jiffies, jstop));
+}
+
+static void rs_flush_buffer(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = 0;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+}
+
+static int rs_write(struct tty_struct * tty,
+ const unsigned char *buf, int count)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot) {
+ count = 0;
+ }
+ else {
+ if (count > MAX3100_TX_BUF_N)
+ count = MAX3100_TX_BUF_N;
+ memcpy(s->tx_buf, buf, count);
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = count;
+ }
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ max3100_dowork(s);
+ return count;
+}
+
+static int rs_write_room(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ int ret;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot)
+ ret = 0;
+ else
+ ret = MAX3100_TX_BUF_N;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ return ret;
+}
+
+static int rs_chars_in_buffer(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ int ret;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot)
+ ret = MAX3100_TX_BUF_N;
+ else
+ ret = 0;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ return ret;
+}
+
+static void internal_throttle(struct tty_struct * tty, char ch, int rts)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ u16 tx=0,rx;
+ int old_tx_stopped;
+
+ old_tx_stopped = s->tx_stopped;
+ s->tx_stopped = 1;
+ tx = MAX3100_RD;
+ do { /* wait fot tx buf empty */
+ max3100_sr(s, tx, &rx, 1);
+ } while ((rx & MAX3100_T) == 0);
+
+ if (I_IXOFF(tty)) {
+ tx = MAX3100_WD | s->rts | (ch&0xff);
+ }
+ if (C_CRTSCTS(tty)) {
+ s->rts = rts ? (MAX3100_RTS) : 0;
+ tx = MAX3100_WD | s->rts | MAX3100_TE;
+ }
+ if (tx) {
+ max3100_sr(s, tx, &rx, 1);
+ }
+ s->tx_stopped = old_tx_stopped;
+}
+
+static void rs_throttle(struct tty_struct * tty)
+{
+ internal_throttle(tty, STOP_CHAR(tty), 0);
+}
+
+static void rs_unthrottle(struct tty_struct * tty)
+{
+ internal_throttle(tty, START_CHAR(tty), 1);
+}
+
+static int get_lsr_info(struct max3100_port_s *s, unsigned int *value)
+{
+ unsigned char status;
+ u16 tx=0,rx;
+
+ tx = MAX3100_RD;
+ max3100_sr(s, tx, &rx, 1);
+ status = (rx & MAX3100_CTS) > 0;
+ put_user(status,value);
+ return 0;
+}
+
+static int rs_ioctl(struct tty_struct *tty, struct file * file,
+ unsigned int cmd, unsigned long arg)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ int retval = 0;
+
+ switch (cmd) {
+
+ case TIOCSERGETLSR: /* Get line status register */
+ if (access_ok(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))
+ return get_lsr_info(s, (unsigned int *) arg);
+ return -EFAULT;
+
+ default:
+ retval = -ENOIOCTLCMD ;
+ }
+ return retval;
+}
+
+static void change_speed(struct max3100_port_s *s)
+{
+ unsigned cflag;
+ unsigned i;
+ u32 param_new;
+ u32 param_mask;
+
+ if (!s->tty || !s->tty->termios)
+ return;
+ cflag = s->tty->termios->c_cflag;
+ param_new = param_mask = 0;
+
+ i = cflag & CBAUD;
+ /* note: supposed 3.6864 Mhz xtal */
+ switch(i) {
+ case B300:
+ if (s->crystal) {
+ param_new = 16;
+ }
+ else {
+ dev_warn(&s->spi->dev, "unsupported baud rate!\n");
+ param_new = 15;
+ }
+ break;
+ case B600:
+ param_new = 15;
+ break;
+ case B1200:
+ param_new = 14;
+ break;
+ case B2400:
+ param_new = 13;
+ break;
+ case B4800:
+ param_new = 12;
+ break;
+ case B9600:
+ param_new = 11;
+ break;
+ case B19200:
+ param_new = 10;
+ break;
+ case B38400:
+ param_new = 9;
+ break;
+ case B57600:
+ param_new = 2;
+ break;
+ case B115200:
+ param_new = 1;
+ break;
+ case B230400:
+ if (s->crystal) {
+ param_new = 1;
+ dev_warn(&s->spi->dev, "unsupported baud rate!\n");
+ }
+ else {
+ param_new = 0;
+ }
+ break;
+ default:
+ param_new = 1;
+ dev_warn(&s->spi->dev, "invalid baudrate\n");
+ }
+ param_new = (param_new - s->crystal) & MAX3100_BAUD;
+ param_mask |= MAX3100_BAUD;
+
+ if ((cflag & CSIZE) == CS8) {
+ param_new &= ~MAX3100_L;
+ s->parity &= ~MAX3100_7BIT;
+ }
+ else {
+ param_new |= MAX3100_L;
+ s->parity |= MAX3100_7BIT;
+ }
+ param_mask |= MAX3100_L;
+
+ if (cflag & CSTOPB)
+ param_new |= MAX3100_ST;
+ else
+ param_new &= ~MAX3100_ST;
+ param_mask |= MAX3100_ST;
+
+ if (cflag & PARENB) {
+ param_new |= MAX3100_PE;
+ s->parity |= MAX3100_PARITY_ON;
+ }
+ else {
+ param_new &= ~MAX3100_PE;
+ s->parity &= ~MAX3100_PARITY_ON;
+ }
+ param_mask |= MAX3100_PE;
+
+ if (cflag & PARODD) {
+ s->parity |= MAX3100_PARITY_ODD;
+ }
+ else {
+ s->parity &=~ MAX3100_PARITY_ODD;
+ }
+
+ s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
+ max3100_send_conf(s);
+
+ return;
+}
+
+
+static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+
+ if (tty->termios->c_cflag == old_termios->c_cflag)
+ return;
+
+ change_speed(s);
+
+ if ((old_termios->c_cflag & CRTSCTS) &&
+ !(tty->termios->c_cflag & CRTSCTS)) {
+ max3100_dowork(s);
+ }
+}
+
+static void shutdown(struct max3100_port_s *s)
+{
+ s->force_end_work = 1;
+ if (s->workqueue) {
+ flush_workqueue(s->workqueue);
+ destroy_workqueue(s->workqueue);
+ }
+ if (s->irq)
+ free_irq(s->irq, s);
+ kfree(s->tx_buf);
+ s->tx_buf = NULL;
+ if (s->tty)
+ set_bit(TTY_IO_ERROR, &s->tty->flags);
+}
+
+static void rs_hangup(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+
+ shutdown(s);
+}
+
+static int startup(struct max3100_port_s *s)
+{
+ char b[10];
+ s->tx_buf = kmalloc(MAX3100_TX_BUF_N, GFP_KERNEL);
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = 0;
+
+ s->force_end_work = 0;
+ s->parity = 0;
+ s->rts = 0;
+ s->tx_stopped = 0;
+ s->conf = MAX3100_RM;
+
+ sprintf(b, "max3100-%d", s->minor);
+ s->workqueue = create_singlethread_workqueue(b);
+ if (!s->workqueue) {
+ dev_warn(&s->spi->dev, "cannot create workqueue\n");
+ return -EBUSY;
+ }
+ INIT_WORK(&s->work, max3100_work);
+
+ s->irq_status = 1; /* IRQS are on after request */
+ if (request_irq(s->irq, max3100_irq, 0, "max3100", s) < 0) {
+ dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
+ s->irq = 0;
+ return -EBUSY;
+ }
+ if (s->only_edge_irq)
+ set_irq_type(s->irq, IRQT_FALLING);
+ else
+ set_irq_type(s->irq, IRQT_LOW);
+
+ if (s->tty)
+ clear_bit(TTY_IO_ERROR, &s->tty->flags);
+
+ change_speed(s);
+
+ if (s->loopback) {
+ u16 tx,rx;
+ tx = 0x4001;
+ max3100_sr(s, tx, &rx, 1);
+ }
+
+ return 0;
+}
+
+int rs_open(struct tty_struct *tty, struct file * filp)
+{
+ struct max3100_port_s *s;
+ int retval=0, line;
+
+ line = tty->index;
+
+ if (line >= MAX_MAX3100 || line < 0)
+ return -ENODEV;
+
+ s = max3100s[line];
+
+ if (!s) {
+ printk("inexistent max3100 for index %d\n", line);
+ return -ENODEV;
+ }
+
+ down(&s->sem);
+
+ s->ref_count++;
+
+ if (s->ref_count == 1) {
+ tty->driver_data = s;
+ s->tty = tty;
+
+ retval = startup(s);
+ }
+
+ up(&s->sem);
+
+ return retval;
+}
+
+static void rs_close(struct tty_struct *tty, struct file * filp)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+
+ down(&s->sem);
+
+ s->ref_count--;
+ if (s->ref_count == 0) {
+ rs_waint_until_sent(tty, 5*HZ);
+ shutdown(s);
+ }
+
+ up(&s->sem);
+}
+
+static int rs_tiocmget(struct tty_struct *tty, struct file *file)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ u16 tx,rx;
+ unsigned int result = 0;
+
+ tx = MAX3100_RD;
+ max3100_sr(s, tx, &rx, 1);
+
+ result = ((s->rts) ? TIOCM_RTS : 0)
+ | ((rx & MAX3100_CTS) ? TIOCM_CTS : 0);
+ return result;
+}
+
+static int rs_tiocmset(struct tty_struct *tty, struct file *file,
+ unsigned int set, unsigned int clear)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
+ int old_rts = s->rts;
+ u16 tx,rx;
+
+ if (set & TIOCM_RTS)
+ s->rts = MAX3100_RTS;
+ if (clear & TIOCM_RTS)
+ s->rts = 0;
+
+ if (s->rts != old_rts) {
+ tx = MAX3100_WD | s->rts | MAX3100_TE;
+ max3100_sr(s, tx, &rx, 1);
+ }
+ return 0;
+}
+
+struct tty_operations rs_ops = {
+ .open = rs_open,
+ .close = rs_close,
+ .write = rs_write,
+ .flush_chars = rs_flush_chars,
+ .wait_until_sent = rs_waint_until_sent,
+ .write_room = rs_write_room,
+ .chars_in_buffer = rs_chars_in_buffer,
+ .flush_buffer = rs_flush_buffer,
+ .ioctl = rs_ioctl,
+ .throttle = rs_throttle,
+ .unthrottle = rs_unthrottle,
+ .set_termios = rs_set_termios,
+ .stop = rs_stop,
+ .start = rs_start,
+ .hangup = rs_hangup,
+ .tiocmget = rs_tiocmget,
+ .tiocmset = rs_tiocmset,
+};
+
+static int __devinit max3100_probe(struct spi_device *spi)
+{
+ int i;
+ struct plat_max3100 *pdata;
+
+ if (!serial_driver) {
+ serial_driver = alloc_tty_driver(MAX_MAX3100);
+ if (!serial_driver) {
+ printk("Cannot allocate serial driver\n");
+ return -ENOMEM;
+ }
+
+ serial_driver->name = "ttyMAX";
+ serial_driver->driver_name = "ttyMAX";
+ serial_driver->major = TTY_MAJOR;
+ serial_driver->minor_start = 128; /* this should prevent clashes */
+ serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
+ serial_driver->subtype = SERIAL_TYPE_NORMAL;
+ serial_driver->init_termios = tty_std_termios;
+ serial_driver->init_termios.c_cflag =
+ B9600 | CS8 | CREAD | HUPCL | CLOCAL;
+ serial_driver->flags = TTY_DRIVER_REAL_RAW |TTY_DRIVER_DYNAMIC_DEV;
+ tty_set_operations(serial_driver, &rs_ops);
+
+ if (tty_register_driver(serial_driver)) {
+ put_tty_driver(serial_driver);
+ printk("Couldn't register serial driver\n");
+ return -EINVAL;
+ }
+ }
+
+ for(i=0; i<MAX_MAX3100; i++)
+ if (!max3100s[i])
+ break;
+ if (i == MAX_MAX3100) {
+ dev_warn(&spi->dev, "too many MAX3100 chips\n");
+ return -ENOMEM;
+ }
+
+ max3100s[i] = kmalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
+ if (!max3100s[i]) {
+ dev_warn(&spi->dev, "kmalloc for max3100 structure %d failed!\n", i);
+ return -ENOMEM;
+ }
+ memset(max3100s[i], 0, sizeof(struct max3100_port_s));
+ max3100s[i]->spi = spi;
+ max3100s[i]->irq = spi->irq;
+ init_MUTEX(&max3100s[i]->sem);
+ init_MUTEX(&max3100s[i]->spi_txrx);
+ spin_lock_init(&max3100s[i]->tx_buf_lock);
+ spin_lock_init(&max3100s[i]->irq_lock);
+ dev_set_drvdata(&spi->dev, &max3100s[i]);
+ pdata = spi->dev.platform_data;
+ max3100s[i]->crystal = pdata->crystal;
+ max3100s[i]->loopback = pdata->loopback;
+ max3100s[i]->only_edge_irq = pdata->only_edge_irq;
+ max3100s[i]->minor = i;
+ tty_register_device(serial_driver, i, &spi->dev);
+
+ return 0;
+}
+
+static int __devexit max3100_remove(struct spi_device *spi)
+{
+ int i;
+
+ i = 0;
+
+ for(i=0; i<MAX_MAX3100; i++)
+ if (max3100s[i] && max3100s[i]->ref_count > 0)
+ return -EBUSY;
+
+ for(i=0; i<MAX_MAX3100; i++)
+ if (max3100s[i]) {
+ tty_unregister_device(serial_driver, i);
+ kfree(max3100s[i]);
+ max3100s[i] = NULL;
+ }
+
+ if (serial_driver)
+ tty_unregister_driver(serial_driver);
+ return 0;
+}
+
+#ifdef CONFIG_PM
+static int max3100_suspend(struct spi_device *spi, pm_message_t state)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *) dev_get_drvdata(&spi->dev);
+ u16 tx,rx;
+
+ tx = MAX3100_WC | MAX3100_SHDN;
+ max3100_sr(s, tx, &rx, 0);
+ return 0;
+}
+
+static int max3100_resume(struct spi_device *spi) {
+ struct max3100_port_s *s = (struct max3100_port_s *) dev_get_drvdata(&spi->dev);
+
+ max3100_send_conf(s);
+
+ return 0;
+}
+
+#else
+#define max3100_suspend NULL
+#define max3100_resume NULL
+#endif
+
+static struct spi_driver max3100_driver = {
+ .driver = {
+ .name = "max3100",
+ .bus = &spi_bus_type,
+ .owner = THIS_MODULE,
+ },
+
+ .probe = max3100_probe,
+ .remove = __devexit_p(max3100_remove),
+ .suspend = max3100_suspend,
+ .resume = max3100_resume,
+};
+
+
+static int __init max3100_init(void)
+{
+ return spi_register_driver(&max3100_driver);
+}
+module_init(max3100_init);
+
+static void __exit max3100_exit(void)
+{
+ spi_unregister_driver(&max3100_driver);
+}
+module_exit(max3100_exit);
+
+MODULE_DESCRIPTION("MAX3100 driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/serial_max3100.h b/include/linux/serial_max3100.h
new file mode 100644
index 0000000..eefac54
--- /dev/null
+++ b/include/linux/serial_max3100.h
@@ -0,0 +1,25 @@
+
+/*
+ *
+ * Copyright (C) 2007 Christian Pellegrin
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#ifndef _LINUX_SERIAL_MAX3100_H
+#define _LINUX_SERIAL_MAX3100_H 1
+
+struct plat_max3100 {
+ int loopback;
+/* force MAX3100 in loopback */
+ int crystal;
+/* 0 for 3.6864 Mhz, 1 for 1.8432 */
+ int only_edge_irq;
+/* for archs like PXA with only edge irqs */
+};
+
+#endif
--
1.4.4.4
^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2007-12-05 10:26 [PATCH] max3100 driver chripell
@ 2007-12-12 9:49 ` Andrew Morton
2007-12-12 11:58 ` chri
0 siblings, 1 reply; 21+ messages in thread
From: Andrew Morton @ 2007-12-12 9:49 UTC (permalink / raw)
To: chripell; +Cc: linux-kernel, Christian Pellegrin
On Wed, 5 Dec 2007 11:26:45 +0100 chripell@gmail.com wrote:
> This patch adds support for the MAX3100 SPI UART.
>
Doesn't compile on x86_64:
drivers/serial/max3100.c: In function 'startup':
drivers/serial/max3100.c:706: error: 'IRQT_FALLING' undeclared (first use in this function)
drivers/serial/max3100.c:706: error: (Each undeclared identifier is reported only once
drivers/serial/max3100.c:706: error: for each function it appears in.)
drivers/serial/max3100.c:708: error: 'IRQT_LOW' undeclared (first use in this function)
> include/linux/serial_max3100.h | 25 +
> 4 files changed, 993 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
> index 81b52b7..4e7111b 100644
> --- a/drivers/serial/Kconfig
> +++ b/drivers/serial/Kconfig
> @@ -461,6 +461,13 @@ config SERIAL_S3C2410_CONSOLE
> your boot loader about how to pass options to the kernel at
> boot time.)
>
> +config SERIAL_MAX3100
> + tristate "MAX3100 support"
> + depends on SPI
> + help
> + MAX3100 chip support
> +
Putting a depends on ARM in here might be sufficient.
> +++ b/drivers/serial/max3100.c
> @@ -0,0 +1,960 @@
> +
> +/*
> + *
> + * Copyright (C) 2007 Christian Pellegrin <chripell@evolware.org>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * version 0.1 31/5/2006 first public release
> + * version 0.2 11/11/2007 ported to kernel 2.6.23
Normally we skip info like this - it's all in the git tree, in better form.
> +static struct tty_driver *serial_driver = NULL;
This initialisation is unneeded and is undesirable because it increases the
kernel image's .data.
Please run the diff through scripts/checkpatch.pl. It would have mentioned
this, and a whole lot of other things, too.
> +/* global since we do register the driver only once */
I assume this comment refers to the preceding line?
Please put comments like this above the code which they're talking about.
Doing it this way is quite confusing to kernel developers.
> +#define MAX_MAX3100 4 /* 4 MAX3100s should be enough for everyone */
> +static struct max3100_port_s * max3100s[MAX_MAX3100]; /* the chips */
No space after the *, please (checkpatch should detect this).
> +
> +#define MAX3100_TX_BUF_N 16
> +/* our buffer for sendind chars */
Typo. Misplaced comment.
> +#define MAX3100_FLIP_EVERY 8
> +/* how many chars we wait befor flipping tty buffer */
Misplaced comment.
> +static void irq_state(struct max3100_port_s *s, int on) {
That brace should go in column 1.
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->irq_lock, flags);
> + if (s->irq_status != on) {
> + if (on) {
> + enable_irq(s->irq);
> + }
> + else {
> + disable_irq(s->irq);
> + }
The `else' should be like this:
} else {
and as the brqaces are unneeded we'd prefer
if (on)
enable_irq(s->irq);
else
disable_irq(s->irq);
but checkpatch knows about this too.
> + s->irq_status = on;
> + }
> + spin_unlock_irqrestore(&s->irq_lock, flags);
> +}
> +
> +
> +static int max3100_do_parity(struct max3100_port_s *s, u16 c)
> +{
> + int parity;
> + int i,n;
> +
> + if (s->parity & MAX3100_PARITY_ODD)
> + parity = 0;
> + else
> + parity = 1;
> +
> + if (s->parity & MAX3100_7BIT)
> + n = 7;
> + else
> + n = 8;
> +
> + for(i=0;i<n;i++) {
> + parity = parity ^ ( (c>>i) & 1);
> + }
Those three lines will cause a checkpatch frenzy.
> + return parity;
> +}
> +
> +static inline int max3100_check_parity(struct max3100_port_s *s, u16 c)
> +{
> + return max3100_do_parity(s, c) == ( (c>>9) & 1);
> +}
> +
> +static inline void max3100_calc_parity(struct max3100_port_s *s, u16 *c)
> +{
> + *c &= ~MAX3100_PT;
> + *c |= max3100_do_parity(s, *c)<<8;
> +}
> +
> +
> +static void max3100_work(struct work_struct *w);
> +
> +static inline void max3100_dowork(struct max3100_port_s *s)
> +{
> + if (!s->force_end_work) {
> + PREPARE_WORK(&s->work, max3100_work);
> + queue_work(s->workqueue, &s->work);
> + }
> +}
You'd be surprised how small an inlined function can be and yet still be
too large to be inlined (if that sentence makes sense).
I'd suggest that you try removing inlines then recmpiling and doing `size
drivers/serial/max3100.o'. You'll probably find that most of them are
either unneeded or plain wrong.
> +static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx, int push)
> +{
> + /* note: we suppose that the T bit fron conf_status is in sync
> + * with the hw one, so all the communication must go through
> + * this function */
> + struct spi_message message;
> + struct spi_transfer x;
> + int status, flag;
> + u16 etx,erx;
> + int got_char = 0;
> +
> + down(&s->spi_txrx);
The semaphore API is deprecated unless one is actually using its counting
feature. Please prefer to use struct mutex.
I'd have expected checkpatch to warn about that too...
> + if (s->loopback) {
> + if ((tx & MAX3100_CMD) == MAX3100_RC)
> + tx |= 1;
> + }
> + etx = htons(tx);
> + spi_message_init(&message);
> + memset(&x, 0, sizeof(x));
> + x.tx_buf = &etx;
> + x.rx_buf = &erx;
> + x.len = 2;
`x' is a truly awful identifier. Please try to come up with something
which means something?
> + spi_message_add_tail(&x, &message);
> + status = spi_sync(s->spi, &message);
> + if (status) {
> + dev_warn(&s->spi->dev, "error while calling spi_sync\n");
> + return 0;
> + }
> + *rx = ntohs(erx);
> + //printk("%04x - %04x\n", tx, *rx);
checkpatch won't like the c99-style comment.
> + if ((tx & MAX3100_CMD) == MAX3100_RD ||
> + (tx & MAX3100_CMD) == MAX3100_WD) {
> + s->last_cts_rx = *rx;
> + }
> +
> + if (*rx & MAX3100_R &&
> + ((tx & MAX3100_CMD) == MAX3100_RD ||
> + (tx & MAX3100_CMD) == MAX3100_WD)) {
> + unsigned int flags;
> +
> + got_char = 1;
> + if (tty_buffer_request_room(s->tty, 1) == 0) {
> + dev_warn(&s->spi->dev, "no room in tty buffer\n");
> + tty_schedule_flip(s->tty);
> + }
> +
> + flag = TTY_NORMAL;
> + if (*rx & MAX3100_RAFE) {
> + s->port.icount.frame++;
> + flags = TTY_FRAME;
> + }
> + if (s->parity & MAX3100_PARITY_ON && max3100_check_parity(s, *rx)) {
> + s->port.icount.parity++;
> + flags = TTY_PARITY;
> + }
> + tty_insert_flip_char(s->tty, *rx & MAX3100_DATA, flag);
Local variable 'flags' never gets used. If it _did_ get used, you'd get a
used-uninitialised-variable warning.
> + }
> +
> + up(&s->spi_txrx);
> +
> + if (push && got_char) {
> + tty_schedule_flip(s->tty);
> + }
unneeded braces.
> + return got_char;
> +}
> +
> +
> +static void max3100_send_conf(struct max3100_port_s *s)
> +{
> + u16 tx=0,rx;
> +
> + tx = MAX3100_WC | (s->conf & 0x0fff);
> + max3100_sr(s, tx, &rx, 1);
> +}
> +
> +static inline int max3100_ok_to_send(struct max3100_port_s *s)
> +{
> + if (C_CRTSCTS(s->tty)) {
> + if ((s->last_cts_rx & MAX3100_CTS) == 0)
> + return 0;
> + }
> + return ! s->tx_stopped;
> +}
> +
> +static void max3100_work(struct work_struct *w)
> +{
> + struct max3100_port_s *s = container_of(w, struct max3100_port_s, work);
> + u16 tx,rx;
> + unsigned long flags;
> + int nchars; /* number of char waiting in the send buf */
> + int rxchars = 0; /* chars received */
> +
> + do {
local variable `flags' could be local to this block. I think that
restricting variable scope in large functions like this improves
readability.
> + tx = MAX3100_RD;
> + rxchars += max3100_sr(s, tx, &rx, 0);
> + /* rx a char so we get MAX3100_T and CTS current */
> +
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + nchars = s->tx_buf_tot - s->tx_buf_cur;
> + tx = MAX3100_WD | s->rts;
> + tx |= s->tx_buf[s->tx_buf_cur];
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> +
> + if ((rx & MAX3100_T) && /* tx buffer empty*/
> + max3100_ok_to_send(s) && /* fw ctrl decision */
> + nchars > 0 && /* more to send */
> + ! s->force_end_work) {
> + if (s->parity & MAX3100_PARITY_ON)
> + max3100_calc_parity(s, &tx);
> + rxchars += max3100_sr(s, tx, &rx, 0);
> + nchars -= 1;
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + s->tx_buf_cur += 1;
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> + if (nchars == 0) {
> + tty_wakeup(s->tty);
> + }
> + }
> + if (rxchars > MAX3100_FLIP_EVERY) {
> + tty_schedule_flip(s->tty);
> + rxchars = 0;
> + }
> + }
> + while ((nchars > 0 || /* more to send */
> + rx & MAX3100_R) && /* try to empy all the RX buffer */
> + ! s->force_end_work);
> + if (rxchars) {
> + tty_schedule_flip(s->tty);
> + }
> + if (! s->only_edge_irq && ! s->force_end_work)
No space after the !, please (checkpatch)
This patch adds large amounts of trailing whitespace. I always chop that
off, but it's best that it never arrives, please.
> + irq_state(s, 1);
> +}
> +
> +static irqreturn_t max3100_irq(int irqno, void *dev_id)
> +{
> + struct max3100_port_s *s = dev_id;
> +
> + if (! s->only_edge_irq)
> + irq_state(s, 0); /* avoid irq storm */
> + max3100_dowork(s);
> + return IRQ_HANDLED;
> +}
> +
> +static void rs_stop(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> +
> + s->tx_stopped = 1;
> +}
> +
> +static void rs_start(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> +
> + s->tx_stopped = 0;
> +}
> +
> +static void rs_flush_chars(struct tty_struct *tty)
> +{
> +}
> +
> +static void rs_waint_until_sent(struct tty_struct *tty, int timeout)
waint?
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + unsigned long jstop;
> +
> + jstop = jiffies + timeout;
> + do {
> + if (s->tx_buf_tot == 0 || s->tx_buf_tot == s->tx_buf_cur)
> + return;
> + schedule();
> + } while (!timeout || time_before(jiffies, jstop));
> +}
eek, that's a busy-wait. Can something smarter be done here?
> +static void rs_flush_buffer(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + s->tx_buf_cur = 0;
> + s->tx_buf_tot = 0;
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> +}
> +
> +static int rs_write(struct tty_struct * tty,
> + const unsigned char *buf, int count)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
Unneded cast of void*. This is unsightly and defeats typechecking. PLease
just remove the cast (several instances of this).
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + if (s->tx_buf_cur < s->tx_buf_tot) {
> + count = 0;
> + }
> + else {
checkpatch...
> + if (count > MAX3100_TX_BUF_N)
> + count = MAX3100_TX_BUF_N;
> + memcpy(s->tx_buf, buf, count);
> + s->tx_buf_cur = 0;
> + s->tx_buf_tot = count;
> + }
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> + max3100_dowork(s);
> + return count;
> +}
> +
> +static int rs_write_room(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + int ret;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + if (s->tx_buf_cur < s->tx_buf_tot)
> + ret = 0;
> + else
> + ret = MAX3100_TX_BUF_N;
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> + return ret;
> +}
> +
> +static int rs_chars_in_buffer(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + int ret;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->tx_buf_lock, flags);
> + if (s->tx_buf_cur < s->tx_buf_tot)
> + ret = MAX3100_TX_BUF_N;
> + else
> + ret = 0;
> + spin_unlock_irqrestore(&s->tx_buf_lock, flags);
> + return ret;
> +}
> +
> +static void internal_throttle(struct tty_struct * tty, char ch, int rts)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + u16 tx=0,rx;
> + int old_tx_stopped;
> +
> + old_tx_stopped = s->tx_stopped;
> + s->tx_stopped = 1;
> + tx = MAX3100_RD;
> + do { /* wait fot tx buf empty */
typo in comment.
> + max3100_sr(s, tx, &rx, 1);
> + } while ((rx & MAX3100_T) == 0);
Another busy-wait. Avoidable?
> + if (I_IXOFF(tty)) {
> + tx = MAX3100_WD | s->rts | (ch&0xff);
> + }
> + if (C_CRTSCTS(tty)) {
> + s->rts = rts ? (MAX3100_RTS) : 0;
> + tx = MAX3100_WD | s->rts | MAX3100_TE;
> + }
> + if (tx) {
> + max3100_sr(s, tx, &rx, 1);
> + }
> + s->tx_stopped = old_tx_stopped;
> +}
> +
> +static void rs_throttle(struct tty_struct * tty)
> +{
> + internal_throttle(tty, STOP_CHAR(tty), 0);
> +}
> +
> +static void rs_unthrottle(struct tty_struct * tty)
> +{
> + internal_throttle(tty, START_CHAR(tty), 1);
> +}
> +
> +static int get_lsr_info(struct max3100_port_s *s, unsigned int *value)
> +{
> + unsigned char status;
> + u16 tx=0,rx;
> +
> + tx = MAX3100_RD;
> + max3100_sr(s, tx, &rx, 1);
> + status = (rx & MAX3100_CTS) > 0;
> + put_user(status,value);
> + return 0;
> +}
> +
> +static int rs_ioctl(struct tty_struct *tty, struct file * file,
> + unsigned int cmd, unsigned long arg)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + int retval = 0;
> +
> + switch (cmd) {
> +
> + case TIOCSERGETLSR: /* Get line status register */
> + if (access_ok(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))
> + return get_lsr_info(s, (unsigned int *) arg);
get_lsr_info() uses put_user() which already does the access_ok() check, so
it should be removed from here.
get_lsr_info() should be changed to return the return value from put_user().
> + return -EFAULT;
> +
> + default:
> + retval = -ENOIOCTLCMD ;
> + }
> + return retval;
> +}
> +
> +static void change_speed(struct max3100_port_s *s)
> +{
> + unsigned cflag;
> + unsigned i;
> + u32 param_new;
> + u32 param_mask;
> +
> + if (!s->tty || !s->tty->termios)
> + return;
Can this happen?
> + cflag = s->tty->termios->c_cflag;
> + param_new = param_mask = 0;
> +
> + i = cflag & CBAUD;
> + /* note: supposed 3.6864 Mhz xtal */
> + switch(i) {
> + case B300:
> + if (s->crystal) {
> + param_new = 16;
> + }
> + else {
> + dev_warn(&s->spi->dev, "unsupported baud rate!\n");
> + param_new = 15;
> + }
> + break;
> + case B600:
> + param_new = 15;
> + break;
> + case B1200:
> + param_new = 14;
> + break;
> + case B2400:
> + param_new = 13;
> + break;
> + case B4800:
> + param_new = 12;
> + break;
> + case B9600:
> + param_new = 11;
> + break;
> + case B19200:
> + param_new = 10;
> + break;
> + case B38400:
> + param_new = 9;
> + break;
> + case B57600:
> + param_new = 2;
> + break;
> + case B115200:
> + param_new = 1;
> + break;
> + case B230400:
> + if (s->crystal) {
> + param_new = 1;
> + dev_warn(&s->spi->dev, "unsupported baud rate!\n");
> + }
> + else {
> + param_new = 0;
> + }
> + break;
> + default:
> + param_new = 1;
> + dev_warn(&s->spi->dev, "invalid baudrate\n");
> + }
> + param_new = (param_new - s->crystal) & MAX3100_BAUD;
> + param_mask |= MAX3100_BAUD;
> +
> + if ((cflag & CSIZE) == CS8) {
> + param_new &= ~MAX3100_L;
> + s->parity &= ~MAX3100_7BIT;
> + }
> + else {
> + param_new |= MAX3100_L;
> + s->parity |= MAX3100_7BIT;
> + }
> + param_mask |= MAX3100_L;
> +
> + if (cflag & CSTOPB)
> + param_new |= MAX3100_ST;
> + else
> + param_new &= ~MAX3100_ST;
> + param_mask |= MAX3100_ST;
> +
> + if (cflag & PARENB) {
> + param_new |= MAX3100_PE;
> + s->parity |= MAX3100_PARITY_ON;
> + }
> + else {
> + param_new &= ~MAX3100_PE;
> + s->parity &= ~MAX3100_PARITY_ON;
> + }
> + param_mask |= MAX3100_PE;
> +
> + if (cflag & PARODD) {
> + s->parity |= MAX3100_PARITY_ODD;
> + }
> + else {
> + s->parity &=~ MAX3100_PARITY_ODD;
> + }
Lots of checkpatch fodder there.
> + s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
> + max3100_send_conf(s);
> +
> + return;
> +}
> +
> +
> +static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> +
> + if (tty->termios->c_cflag == old_termios->c_cflag)
> + return;
> +
> + change_speed(s);
> +
> + if ((old_termios->c_cflag & CRTSCTS) &&
> + !(tty->termios->c_cflag & CRTSCTS)) {
> + max3100_dowork(s);
> + }
> +}
Something went wrong with whitespace in rs_set_termios().
> +static void shutdown(struct max3100_port_s *s)
> +{
> + s->force_end_work = 1;
> + if (s->workqueue) {
> + flush_workqueue(s->workqueue);
> + destroy_workqueue(s->workqueue);
> + }
> + if (s->irq)
> + free_irq(s->irq, s);
> + kfree(s->tx_buf);
> + s->tx_buf = NULL;
> + if (s->tty)
> + set_bit(TTY_IO_ERROR, &s->tty->flags);
> +}
> +
> +static void rs_hangup(struct tty_struct *tty)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
unneeded cast..
> + shutdown(s);
> +}
> +
> +static int startup(struct max3100_port_s *s)
> +{
> + char b[10];
> + s->tx_buf = kmalloc(MAX3100_TX_BUF_N, GFP_KERNEL);
Unchecked kmalloc().
> + s->tx_buf_cur = 0;
> + s->tx_buf_tot = 0;
> +
> + s->force_end_work = 0;
> + s->parity = 0;
> + s->rts = 0;
> + s->tx_stopped = 0;
> + s->conf = MAX3100_RM;
> +
> + sprintf(b, "max3100-%d", s->minor);
> + s->workqueue = create_singlethread_workqueue(b);
> + if (!s->workqueue) {
> + dev_warn(&s->spi->dev, "cannot create workqueue\n");
> + return -EBUSY;
> + }
> + INIT_WORK(&s->work, max3100_work);
> +
> + s->irq_status = 1; /* IRQS are on after request */
> + if (request_irq(s->irq, max3100_irq, 0, "max3100", s) < 0) {
No need for IRQ sharing?
> + dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
> + s->irq = 0;
> + return -EBUSY;
We just leaked the memory at s->tx_buf.
> + }
> + if (s->only_edge_irq)
> + set_irq_type(s->irq, IRQT_FALLING);
> + else
> + set_irq_type(s->irq, IRQT_LOW);
> +
> + if (s->tty)
> + clear_bit(TTY_IO_ERROR, &s->tty->flags);
> +
> + change_speed(s);
> +
> + if (s->loopback) {
> + u16 tx,rx;
> + tx = 0x4001;
> + max3100_sr(s, tx, &rx, 1);
> + }
> +
> + return 0;
> +}
> +
> +int rs_open(struct tty_struct *tty, struct file * filp)
> +{
> + struct max3100_port_s *s;
> + int retval=0, line;
> +
> + line = tty->index;
> +
> + if (line >= MAX_MAX3100 || line < 0)
> + return -ENODEV;
> +
> + s = max3100s[line];
> +
> + if (!s) {
> + printk("inexistent max3100 for index %d\n", line);
"Nonexistent"
> + return -ENODEV;
> + }
> +
> + down(&s->sem);
> +
> + s->ref_count++;
> +
> + if (s->ref_count == 1) {
> + tty->driver_data = s;
> + s->tty = tty;
> +
> + retval = startup(s);
> + }
> +
> + up(&s->sem);
> +
> + return retval;
> +}
> +
> +static void rs_close(struct tty_struct *tty, struct file * filp)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> +
> + down(&s->sem);
> +
> + s->ref_count--;
> + if (s->ref_count == 0) {
> + rs_waint_until_sent(tty, 5*HZ);
hm, a magic constant. What's happening here?
> + shutdown(s);
> + }
> +
> + up(&s->sem);
> +}
> +
> +static int rs_tiocmget(struct tty_struct *tty, struct file *file)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + u16 tx,rx;
> + unsigned int result = 0;
unneeded initialisation.
> + tx = MAX3100_RD;
> + max3100_sr(s, tx, &rx, 1);
> +
> + result = ((s->rts) ? TIOCM_RTS : 0)
> + | ((rx & MAX3100_CTS) ? TIOCM_CTS : 0);
> + return result;
> +}
> +
> +static int rs_tiocmset(struct tty_struct *tty, struct file *file,
> + unsigned int set, unsigned int clear)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) tty->driver_data;
> + int old_rts = s->rts;
> + u16 tx,rx;
> +
> + if (set & TIOCM_RTS)
> + s->rts = MAX3100_RTS;
> + if (clear & TIOCM_RTS)
> + s->rts = 0;
> +
> + if (s->rts != old_rts) {
> + tx = MAX3100_WD | s->rts | MAX3100_TE;
> + max3100_sr(s, tx, &rx, 1);
> + }
> + return 0;
> +}
> +
> +struct tty_operations rs_ops = {
> + .open = rs_open,
> + .close = rs_close,
> + .write = rs_write,
> + .flush_chars = rs_flush_chars,
> + .wait_until_sent = rs_waint_until_sent,
> + .write_room = rs_write_room,
> + .chars_in_buffer = rs_chars_in_buffer,
> + .flush_buffer = rs_flush_buffer,
> + .ioctl = rs_ioctl,
> + .throttle = rs_throttle,
> + .unthrottle = rs_unthrottle,
> + .set_termios = rs_set_termios,
> + .stop = rs_stop,
> + .start = rs_start,
> + .hangup = rs_hangup,
> + .tiocmget = rs_tiocmget,
> + .tiocmset = rs_tiocmset,
> +};
Can this have static scope?
> +static int __devinit max3100_probe(struct spi_device *spi)
> +{
> + int i;
> + struct plat_max3100 *pdata;
> +
> + if (!serial_driver) {
> + serial_driver = alloc_tty_driver(MAX_MAX3100);
> + if (!serial_driver) {
> + printk("Cannot allocate serial driver\n");
> + return -ENOMEM;
> + }
> +
> + serial_driver->name = "ttyMAX";
> + serial_driver->driver_name = "ttyMAX";
> + serial_driver->major = TTY_MAJOR;
> + serial_driver->minor_start = 128; /* this should prevent clashes */
> + serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
> + serial_driver->subtype = SERIAL_TYPE_NORMAL;
> + serial_driver->init_termios = tty_std_termios;
> + serial_driver->init_termios.c_cflag =
> + B9600 | CS8 | CREAD | HUPCL | CLOCAL;
> + serial_driver->flags = TTY_DRIVER_REAL_RAW |TTY_DRIVER_DYNAMIC_DEV;
> + tty_set_operations(serial_driver, &rs_ops);
> +
> + if (tty_register_driver(serial_driver)) {
> + put_tty_driver(serial_driver);
> + printk("Couldn't register serial driver\n");
> + return -EINVAL;
> + }
> + }
> +
> + for(i=0; i<MAX_MAX3100; i++)
> + if (!max3100s[i])
> + break;
> + if (i == MAX_MAX3100) {
> + dev_warn(&spi->dev, "too many MAX3100 chips\n");
> + return -ENOMEM;
> + }
> +
> + max3100s[i] = kmalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
> + if (!max3100s[i]) {
> + dev_warn(&spi->dev, "kmalloc for max3100 structure %d failed!\n", i);
> + return -ENOMEM;
> + }
> + memset(max3100s[i], 0, sizeof(struct max3100_port_s));
use kzalloc() above, remove the memset.
> + max3100s[i]->spi = spi;
> + max3100s[i]->irq = spi->irq;
> + init_MUTEX(&max3100s[i]->sem);
> + init_MUTEX(&max3100s[i]->spi_txrx);
Use struct mutex.
> + spin_lock_init(&max3100s[i]->tx_buf_lock);
> + spin_lock_init(&max3100s[i]->irq_lock);
> + dev_set_drvdata(&spi->dev, &max3100s[i]);
> + pdata = spi->dev.platform_data;
> + max3100s[i]->crystal = pdata->crystal;
> + max3100s[i]->loopback = pdata->loopback;
> + max3100s[i]->only_edge_irq = pdata->only_edge_irq;
> + max3100s[i]->minor = i;
> + tty_register_device(serial_driver, i, &spi->dev);
> +
> + return 0;
> +}
> +
> +static int __devexit max3100_remove(struct spi_device *spi)
> +{
> + int i;
> +
> + i = 0;
> +
> + for(i=0; i<MAX_MAX3100; i++)
> + if (max3100s[i] && max3100s[i]->ref_count > 0)
> + return -EBUSY;
> +
> + for(i=0; i<MAX_MAX3100; i++)
> + if (max3100s[i]) {
> + tty_unregister_device(serial_driver, i);
> + kfree(max3100s[i]);
> + max3100s[i] = NULL;
> + }
> +
> + if (serial_driver)
> + tty_unregister_driver(serial_driver);
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int max3100_suspend(struct spi_device *spi, pm_message_t state)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *) dev_get_drvdata(&spi->dev);
> + u16 tx,rx;
> +
> + tx = MAX3100_WC | MAX3100_SHDN;
> + max3100_sr(s, tx, &rx, 0);
> + return 0;
> +}
> +
> +static int max3100_resume(struct spi_device *spi) {
> + struct max3100_port_s *s = (struct max3100_port_s *) dev_get_drvdata(&spi->dev);
> +
> + max3100_send_conf(s);
> +
> + return 0;
> +}
> +
> +#else
> +#define max3100_suspend NULL
> +#define max3100_resume NULL
> +#endif
> +
> +static struct spi_driver max3100_driver = {
> + .driver = {
> + .name = "max3100",
> + .bus = &spi_bus_type,
> + .owner = THIS_MODULE,
> + },
> +
> + .probe = max3100_probe,
> + .remove = __devexit_p(max3100_remove),
> + .suspend = max3100_suspend,
> + .resume = max3100_resume,
> +};
> +
> +
> +static int __init max3100_init(void)
> +{
> + return spi_register_driver(&max3100_driver);
> +}
> +module_init(max3100_init);
> +
> +static void __exit max3100_exit(void)
> +{
> + spi_unregister_driver(&max3100_driver);
> +}
> +module_exit(max3100_exit);
> +
> +MODULE_DESCRIPTION("MAX3100 driver");
> +MODULE_LICENSE("GPL");
> diff --git a/include/linux/serial_max3100.h b/include/linux/serial_max3100.h
> new file mode 100644
> index 0000000..eefac54
> --- /dev/null
> +++ b/include/linux/serial_max3100.h
> @@ -0,0 +1,25 @@
> +
Stray blank line at start of file(s)
> +/*
> + *
> + * Copyright (C) 2007 Christian Pellegrin
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + */
> +
> +
> +#ifndef _LINUX_SERIAL_MAX3100_H
> +#define _LINUX_SERIAL_MAX3100_H 1
> +
> +struct plat_max3100 {
> + int loopback;
> +/* force MAX3100 in loopback */
> + int crystal;
> +/* 0 for 3.6864 Mhz, 1 for 1.8432 */
> + int only_edge_irq;
> +/* for archs like PXA with only edge irqs */
> +};
Does this header file need to exist? afaict plat_max3100 is only used by
drivers/serial/max3100.c and hence could be defined there?
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2007-12-12 9:49 ` Andrew Morton
@ 2007-12-12 11:58 ` chri
0 siblings, 0 replies; 21+ messages in thread
From: chri @ 2007-12-12 11:58 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel
Hi,
thank you for your extensive review, I will fix and resubmitt. Anyway
I learned an important lesson: checkpatch.pl will be my best friend.
Sorry I haven't read about it earlier.
Just a question:
>
> > +/*
> > + *
> > + * Copyright (C) 2007 Christian Pellegrin
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License as published by
> > + * the Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > + */
> > +
> > +
> > +#ifndef _LINUX_SERIAL_MAX3100_H
> > +#define _LINUX_SERIAL_MAX3100_H 1
> > +
> > +struct plat_max3100 {
> > + int loopback;
> > +/* force MAX3100 in loopback */
> > + int crystal;
> > +/* 0 for 3.6864 Mhz, 1 for 1.8432 */
> > + int only_edge_irq;
> > +/* for archs like PXA with only edge irqs */
> > +};
>
> Does this header file need to exist? afaict plat_max3100 is only used by
> drivers/serial/max3100.c and hence could be defined there?
>
It's used in the machine definition file for platform devices that
cannot be autoprobed. Anyway where is the best place to put such
struct definition / include files? And what is the best way to
document them (if not inline)? For example I put in my machine
definition file (let's say arch/arm/mach-s3c2410/mach-smdk2410.c):
static struct plat_max3100 max3100_plat_data = {
.loopback = 0,
.crystal = 0,
.only_edge_irq = 0,
};
static struct spi_board_info spi_board_info[] = {
{
.modalias = "max3100",
.platform_data = &max3100_plat_data,
.irq = IRQ_EINT12,
.max_speed_hz = 5*1000*1000,
.chip_select = 0,
},
};
...
spi_register_board_info(spi_board_info, ARRAY_SIZE(spi_board_info));
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH] max3100 driver
2008-09-20 8:24 ` Andrew Morton
` (2 preceding siblings ...)
2008-10-09 6:23 ` chri
@ 2008-10-10 12:08 ` Christian Pellegrin
3 siblings, 0 replies; 21+ messages in thread
From: Christian Pellegrin @ 2008-10-10 12:08 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, linux-serial
Hi,
This patch fixes a problem about PM in my code that I found when testing
patches that went in -mm. The full patch against the Linus tree can be
found at http://www.evolware.org/chri/paciugo/
---
diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
index 7a269a6..462d6a4 100644
--- a/drivers/serial/max3100.c
+++ b/drivers/serial/max3100.c
@@ -566,6 +566,7 @@ static void max3100_shutdown(struct uart_port *port)
if (s->workqueue) {
flush_workqueue(s->workqueue);
destroy_workqueue(s->workqueue);
+ s->workqueue = NULL;
}
if (s->irq)
free_irq(s->irq, s);
@@ -614,6 +615,7 @@ static int max3100_startup(struct uart_port *port)
dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
s->irq = 0;
destroy_workqueue(s->workqueue);
+ s->workqueue = NULL;
return -EBUSY;
}
@@ -884,7 +886,8 @@ static int max3100_resume(struct spi_device *spi)
enable_irq(s->irq);
s->conf_commit = 1;
- max3100_dowork(s);
+ if (s->workqueue)
+ max3100_dowork(s);
return 0;
}
^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-10-09 6:30 ` chri
@ 2008-10-09 9:18 ` Alan Cox
0 siblings, 0 replies; 21+ messages in thread
From: Alan Cox @ 2008-10-09 9:18 UTC (permalink / raw)
To: chri; +Cc: linux-kernel, linux-serial
> > If there has been a hangup the port.tty will be NULL...
> >
>
> I check against NULL. But let me ask again: the other drivers (for
> example SA1100) don't do it. How they avoid the nasty NULL pointer
> dereference?
Some drives keep the tty pointer and deliver data to closed ttys wrongly,
others do careful locking, others are just broken.
> Sorry again for not having replied before resending the patch.
No problem
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 14:11 ` Alan Cox
2008-09-20 14:37 ` chri
@ 2008-10-09 6:30 ` chri
2008-10-09 9:18 ` Alan Cox
1 sibling, 1 reply; 21+ messages in thread
From: chri @ 2008-10-09 6:30 UTC (permalink / raw)
To: Alan Cox; +Cc: linux-kernel, linux-serial
On Sat, Sep 20, 2008 at 4:11 PM, Alan Cox <alan@lxorguk.ukuu.org.uk> wrote:
>> +#define MAX3100_MAJOR 204
>> +#define MAX3100_MINOR 128
>> +/* 4 MAX3100s should be enough for everyone */
>> +#define MAX_MAX3100 4
>
> These need to be officially allocated if you need constant numbers
>
done, I followed the guide in devices.txt
>> +
>> + etx = htons(tx);
>
> Use cpu_to_le/be or le/be_to_cpu functions, these make the intended
> endianness clear.
>
>> + *rx = ntohs(erx);
>
> Ditto
>
done
>> + if (rxchars > 0)
>> + tty_flip_buffer_push(s->port.info->port.tty);
>> + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
>
> If there has been a hangup the port.tty will be NULL...
>
I check against NULL. But let me ask again: the other drivers (for
example SA1100) don't do it. How they avoid the nasty NULL pointer
dereference?
>> +static void
>> +max3100_set_termios(struct uart_port *port, struct ktermios *termios,
>> + struct ktermios *old)
>> +{
>> + struct max3100_port_s *s = container_of(port,
>> + struct max3100_port_s,
>
>> + if (!old || (termios->c_cflag != old->c_cflag)) {
>
> This optimisation is wrong and not worth doing anyway
>
done
>
> Bits you don't support should also be cleared in the tty->termios struct
> (eg markspace you don't seem to do)
>
>
done, I completely rewrote termios handling following Alan instructions.
>> + max3100s[i] = kzalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
>> + if (!max3100s[i]) {
>> + dev_warn(&spi->dev,
>> + "kmalloc for max3100 structure %d failed!\n", i);
>
> Does this not then need to unregister the driver ?
>
>
I think no: perhaps it's just the probe of the second MAX3100 port
that failed so we cannot unegister the driver. And the user can try to
reprobe the MAX3100 via the bind entry. As I see it: driver
registration and port registration are 2 different things.
>
> Looks basically sound to me - just some minor cleanups needed.
>
> Alan
>
Sorry again for not having replied before resending the patch.
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 8:24 ` Andrew Morton
2008-09-20 10:35 ` chri
2008-09-20 13:56 ` Arjan van de Ven
@ 2008-10-09 6:23 ` chri
2008-10-10 12:08 ` Christian Pellegrin
3 siblings, 0 replies; 21+ messages in thread
From: chri @ 2008-10-09 6:23 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, linux-serial
On Sat, Sep 20, 2008 at 10:24 AM, Andrew Morton
<akpm@linux-foundation.org> wrote:
>> +#define MAX3100_MAJOR 204
>
> Allocating a new major is a Big Deal. It involves getting the major
> registered by contacting device@lanana.org.
>
> It's better to dynamically allocate it - let udev handle it.
>
The MAX3100 is used in just small embedded systems where there is
usually no udev. I looked at other serial drivers but I haven't seen
none that uses dynamic allocation (they are share just 2 major
numbers). I followed the guide in devices.txt and asked for an
allocation of 4 minor numbers in the "Low-density serial ports".
>> +#include <linux/freezer.h>
>
> Gad. Are all those includes needed?
>
cleaned
>> +
>> +struct max3100_port_s {
>
> `struct max3100_port' is sufficient, and would be more typical.
>
done
>> + struct uart_port port;
>> + struct spi_device *spi;
>> +
>> + int cts:1; /* last CTS received for flow ctrl */
>> + int tx_empty:1; /* last TX empty bit */
>
> These two bits will share a word and hence locking is needed to prevent
> modifications to one from trashing modifications to the other on SMP.
>
> That's OK, but it would be best to document that locking right here, and
> to check that it is adhered to.
>
I reverted to using word aligned data.
>> + /* and it's timer */
>
> s/it's/its/
>
done. I just found the useful flyspell-prog-mode so I will drastically
reduce English errors in the patches!
>> + parity = parity ^ ((c>>i) & 1);
>
> hrmpf. Don't we have a library function for that?
>
> Perhaps you could use hweight8(c)&1.
>
done
>> + return parity;
>> +}
>> +
>> +static int max3100_check_parity(struct max3100_port_s *s, u16 c)
>> +{
>> + return max3100_do_parity(s, c) ==
>> + ((c >> (s->parity & MAX3100_7BIT ? 7 : 8)) & 1);
>
> <checks his C precedence table>
>
> OK...
done. Anyway I think that if precedence is unclear to me, it may be
unclear to others too; so I prefer to use a parenthesis too much than
one too few.
>> + *c &= ~MAX3100_PT;
>
> s/ / /
>
done
>
>> +}
>> +
>> +static void max3100_work(struct work_struct *w);
>> +
>> +static void max3100_dowork(struct max3100_port_s *s)
>> +{
>> + if (!s->force_end_work && !work_pending(&s->work)) {
>> + PREPARE_WORK(&s->work, max3100_work);
>
> This is a strange place to run PREPARE_WORK(). Normally it would be
> done during driver/device initialsiation, and I think that can be done
> here.
>
done, I checked the meaning of PREPARE_WORK in the header file.
>> +
>> + etx = htons(tx);
>> + spi_message_init(&message);
>> + memset(&tran, 0, sizeof(tran));
>> + tran.tx_buf = &etx;
>> + tran.rx_buf = &erx;
>> + tran.len = 2;
>
> could do
>
> struct spi_transfer tran = {
> .tx_buf = &etx,
> .rx_buf = &erx
> .len = 2,
> };
>
> and the compiler will zero out the rest. Minor point.
>
done
>> +
>> + dev_dbg(&s->spi->dev, "%s\n", __func__);
>> +
>> + /* may not be truely up-to-date */
>
> s/truely/truly/
>
done, next time flypsell-prog-mode will help me :-)
>> +
>> + sprintf(b, "max3100-%d", s->minor);
>
> hm. This will go nasty if s->minor > 9. I'd suggest that b[] be made
> larger.
>
I limit the number of MAX3100s to 4, anyway I enlarged the buffer by
one so to be sure.
>> + if (request_irq(s->irq, max3100_irq, irq_type, "max3100", s) < 0) {
>> + dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
>> + s->irq = 0;
>
> Shouldn't we tear down that workqueue before returning?
>
done
>> +static void max3100_release_port(struct uart_port *port)
>> +{
>> + struct max3100_port_s *s = (struct max3100_port_s *)port;
>
> container_of()
>
done
Sorry again for not replying in firs time.
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 13:56 ` Arjan van de Ven
2008-09-20 14:30 ` chri
2008-09-20 14:34 ` Alan Cox
@ 2008-09-21 16:09 ` Ben Pfaff
2 siblings, 0 replies; 21+ messages in thread
From: Ben Pfaff @ 2008-09-21 16:09 UTC (permalink / raw)
To: linux-kernel; +Cc: linux-serial
Arjan van de Ven <arjan@infradead.org> writes:
> I do have a question though: what does a signed bitfield of 1 mean?
> I mean.. the variables are "int", so signed.... where will the compiler
> store the sign bit???
Whether a bit-field declared as type "int" is signed or unsigned
is compiler implementation-defined. As C99 6.7.2 says (there is
similar text in C89):
...for bit-fields, it is implementation-defined whether the
specifier int designates the same type as signed int or the
same type as unsigned int.
Thus, it is never a good idea to declare a bit-field as plain
"int". Declare it as "signed int" or "unsigned int" instead.
--
"Mon peu de succès près des femmes est toujours venu de les trop aimer."
--Jean-Jacques Rousseau
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 14:11 ` Alan Cox
@ 2008-09-20 14:37 ` chri
2008-10-09 6:30 ` chri
1 sibling, 0 replies; 21+ messages in thread
From: chri @ 2008-09-20 14:37 UTC (permalink / raw)
To: Alan Cox; +Cc: linux-kernel, linux-serial
On Sat, Sep 20, 2008 at 4:11 PM, Alan Cox <alan@lxorguk.ukuu.org.uk> wrote:
>> +#define MAX3100_MAJOR 204
>> +#define MAX3100_MINOR 128
>> +/* 4 MAX3100s should be enough for everyone */
>> +#define MAX_MAX3100 4
>
> These need to be officially allocated if you need constant numbers
>
OK, I'll try to mail device@lanana.org
>
>> + if (rxchars > 0)
>> + tty_flip_buffer_push(s->port.info->port.tty);
>> + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
>
> If there has been a hangup the port.tty will be NULL...
>
I didn't notice an explicit test for port.tty being non-NULL in some
drivers. I took as an example sa1100 driver (since it needs polling of
control signals too). I'm missing the way these drivers take care to
not cause an access of a NULL pointer?
>
> Looks basically sound to me - just some minor cleanups needed.
>
Thanks, I will fix them and resubmitt
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 13:56 ` Arjan van de Ven
2008-09-20 14:30 ` chri
@ 2008-09-20 14:34 ` Alan Cox
2008-09-21 16:09 ` Ben Pfaff
2 siblings, 0 replies; 21+ messages in thread
From: Alan Cox @ 2008-09-20 14:34 UTC (permalink / raw)
To: Arjan van de Ven
Cc: Andrew Morton, Christian Pellegrin, linux-kernel, linux-serial,
Christian Pellegrin
> I do have a question though: what does a signed bitfield of 1 mean?
> I mean.. the variables are "int", so signed.... where will the compiler
> store the sign bit???
It sign extends so you get 0 or -1. Yes it would be good to use unsigned
to avoid suprises.
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 13:56 ` Arjan van de Ven
@ 2008-09-20 14:30 ` chri
2008-09-20 14:34 ` Alan Cox
2008-09-21 16:09 ` Ben Pfaff
2 siblings, 0 replies; 21+ messages in thread
From: chri @ 2008-09-20 14:30 UTC (permalink / raw)
To: Arjan van de Ven; +Cc: Andrew Morton, linux-kernel, linux-serial
On Sat, Sep 20, 2008 at 3:56 PM, Arjan van de Ven <arjan@infradead.org> wrote:
>
> I do have a question though: what does a signed bitfield of 1 mean?
> I mean.. the variables are "int", so signed.... where will the compiler
> store the sign bit???
>
>
In practice they stored just values 0 and 1 well. 2-complement
representation with 1 bit doesn't have much more space (1 == -1). :-)
Anyway after Andrew comment I stopped using them since I'm too scared:
I'm not sure that it's safe without locking the entire struct.
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 7:20 Christian Pellegrin
2008-09-20 8:24 ` Andrew Morton
@ 2008-09-20 14:11 ` Alan Cox
2008-09-20 14:37 ` chri
2008-10-09 6:30 ` chri
1 sibling, 2 replies; 21+ messages in thread
From: Alan Cox @ 2008-09-20 14:11 UTC (permalink / raw)
To: Christian Pellegrin; +Cc: linux-kernel, linux-serial, Christian Pellegrin
> +#define MAX3100_MAJOR 204
> +#define MAX3100_MINOR 128
> +/* 4 MAX3100s should be enough for everyone */
> +#define MAX_MAX3100 4
These need to be officially allocated if you need constant numbers
> +static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx)
> +{
> + struct spi_message message;
> + struct spi_transfer tran;
> + u16 etx, erx;
> + int status;
> +
> + etx = htons(tx);
Use cpu_to_le/be or le/be_to_cpu functions, these make the intended
endianness clear.
> + *rx = ntohs(erx);
Ditto
> + if (rxchars > 0)
> + tty_flip_buffer_push(s->port.info->port.tty);
> + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
If there has been a hangup the port.tty will be NULL...
> +static void
> +max3100_set_termios(struct uart_port *port, struct ktermios *termios,
> + struct ktermios *old)
> +{
> + struct max3100_port_s *s = container_of(port,
> + struct max3100_port_s,
> + if (!old || (termios->c_cflag != old->c_cflag)) {
This optimisation is wrong and not worth doing anyway
> + i = cflag & CBAUD;
> + switch (i) {
Use tty_get_baud_rate() to get the actual baud rate requested which is an
arbitary value.
> + default:
> + param_new = 1;
> + dev_warn(&s->spi->dev, "invalid baudrate\n");
Shouldn't warn on these, just be sure to use tty_encode_baud_rate to pass
back the actual rate the user ends up with.
> + if (termios->c_iflag & IGNPAR)
> + s->port.ignore_status_mask |=
> + MAX3100_STATUS_PE | MAX3100_STATUS_FE |
> + MAX3100_STATUS_OE;
Bits you don't support should also be cleared in the tty->termios struct
(eg markspace you don't seem to do)
> + max3100s[i] = kzalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
> + if (!max3100s[i]) {
> + dev_warn(&spi->dev,
> + "kmalloc for max3100 structure %d failed!\n", i);
Does this not then need to unregister the driver ?
Looks basically sound to me - just some minor cleanups needed.
Alan
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 8:24 ` Andrew Morton
2008-09-20 10:35 ` chri
@ 2008-09-20 13:56 ` Arjan van de Ven
2008-09-20 14:30 ` chri
` (2 more replies)
2008-10-09 6:23 ` chri
2008-10-10 12:08 ` Christian Pellegrin
3 siblings, 3 replies; 21+ messages in thread
From: Arjan van de Ven @ 2008-09-20 13:56 UTC (permalink / raw)
To: Andrew Morton
Cc: Christian Pellegrin, linux-kernel, linux-serial, Christian Pellegrin
\> > + int rx_enabled:1; /* if we should rx chars */
> > +
> > + int irq; /* irq assigned to the max3100 */
> > +
> > + int minor; /* minor number */
> > + int crystal:1; /* 1 if 3.6864Mhz crystal 0
> > for 1.8432 */
> > + int loopback:1; /* 1 if we are in loopback
> > mode */
> > + int only_edge_irq:1; /* 1 if we have only edge irqs
> > (like PXA) */
>
> Lots of dittoes.
>
> These bitfields perhaps could be reordered to save a bit of space, but
> that depends on the implicit locking rules for them.
>
I do have a question though: what does a signed bitfield of 1 mean?
I mean.. the variables are "int", so signed.... where will the compiler
store the sign bit???
--
Arjan van de Ven Intel Open Source Technology Centre
For development, discussion and tips for power savings,
visit http://www.lesswatts.org
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
@ 2008-09-20 10:51 Michael Trimarchi
0 siblings, 0 replies; 21+ messages in thread
From: Michael Trimarchi @ 2008-09-20 10:51 UTC (permalink / raw)
To: chri, Andrew Morton; +Cc: linux-kernel, linux-serial
Hi,
----- Messaggio originale -----
> Da: chri <chripell@gmail.com>
> A: Andrew Morton <akpm@linux-foundation.org>
> Cc: linux-kernel@vger.kernel.org; linux-serial@vger.kernel.org
> Inviato: Sabato 20 settembre 2008, 12:35:26
> Oggetto: Re: [PATCH] max3100 driver
>
> Sorry, sent HTML mail by mistake, so resending :-/
>
>
> On Sat, Sep 20, 2008 at 10:24 AM, Andrew Morton
> wrote:
>
> > > +#define MAX3100_MAJOR 204
> >
> > Allocating a new major is a Big Deal. It involves getting the major
> > registered by contacting device@lanana.org.
> >
> > It's better to dynamically allocate it - let udev handle it.
> >
>
>
> I looked at other serial driver as an example and checked devices.txt:
> if I don't get it wrong major 204 should be already reserved for
> serial port. Anyway I choose a minor number already allocated by
> mistake (did not see the "...") and will correct that. Is this ok or I
> *have to* move to dynamic major (it's a bit a nuisance since max3100
> is used in embedded system where udev is not always used)?
>
Well, a solution can be to use dynamic allocation if is not specified in
the platform data of the device, so you can statically allocated for
embedded device. So if you don't set the major, the tty layer find one
for you.
Regards Michael
__________________________________________________
Do You Yahoo!?
Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi
http://mail.yahoo.it
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 8:24 ` Andrew Morton
@ 2008-09-20 10:35 ` chri
2008-09-20 13:56 ` Arjan van de Ven
` (2 subsequent siblings)
3 siblings, 0 replies; 21+ messages in thread
From: chri @ 2008-09-20 10:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, linux-serial
Sorry, sent HTML mail by mistake, so resending :-/
On Sat, Sep 20, 2008 at 10:24 AM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> > +#define MAX3100_MAJOR 204
>
> Allocating a new major is a Big Deal. It involves getting the major
> registered by contacting device@lanana.org.
>
> It's better to dynamically allocate it - let udev handle it.
>
I looked at other serial driver as an example and checked devices.txt:
if I don't get it wrong major 204 should be already reserved for
serial port. Anyway I choose a minor number already allocated by
mistake (did not see the "...") and will correct that. Is this ok or I
*have to* move to dynamic major (it's a bit a nuisance since max3100
is used in embedded system where udev is not always used)?
> `struct max3100_port' is sufficient, and would be more typical.
>
> > + struct uart_port port;
> > + struct spi_device *spi;
> > +
> > + int cts:1; /* last CTS received for flow ctrl */
> > + int tx_empty:1; /* last TX empty bit */
>
> These two bits will share a word and hence locking is needed to prevent
> modifications to one from trashing modifications to the other on SMP.
>
> That's OK, but it would be best to document that locking right here, and
> to check that it is adhered to.
>
I did not realize this until you explained me. I'm not sure if actual
packing of bit-fields is implementation dependent but I think so. If
this is right I guess it's better to avoid bit-fields in structs that
can be accessed concurrently (or otherwise I have to lock the entire
struct). So, should I avoid bit-fields altogether?
I will correct the patch and resend.
Thanks,
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2008-09-20 7:20 Christian Pellegrin
@ 2008-09-20 8:24 ` Andrew Morton
2008-09-20 10:35 ` chri
` (3 more replies)
2008-09-20 14:11 ` Alan Cox
1 sibling, 4 replies; 21+ messages in thread
From: Andrew Morton @ 2008-09-20 8:24 UTC (permalink / raw)
To: Christian Pellegrin; +Cc: linux-kernel, linux-serial, Christian Pellegrin
On Sat, 20 Sep 2008 09:20:08 +0200 Christian Pellegrin <chripell@gmail.com> wrote:
> This patch adds support for the MAX3100 SPI UART.
>
>
> ...
>
> + * The initial minor number is 128 in the low-density serial port:
> + * mknod /dev/ttyMAX0 c 204 128
> + */
> +
> +#define MAX3100_MAJOR 204
Allocating a new major is a Big Deal. It involves getting the major
registered by contacting device@lanana.org.
It's better to dynamically allocate it - let udev handle it.
> +#define MAX3100_MINOR 128
> +/* 4 MAX3100s should be enough for everyone */
> +#define MAX_MAX3100 4
> +
> +#include <linux/bitops.h>
> +#include <linux/console.h>
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/errno.h>
> +#include <linux/fcntl.h>
> +#include <linux/init.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/irq.h>
> +#include <linux/kernel.h>
> +#include <linux/keyboard.h>
> +#include <linux/major.h>
> +#include <linux/mm.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/reboot.h>
> +#include <linux/sched.h>
> +#include <linux/serial_core.h>
> +#include <linux/serial.h>
> +#include <linux/signal.h>
> +#include <linux/spi/spi.h>
> +#include <linux/string.h>
> +#include <linux/timer.h>
> +#include <linux/tty.h>
> +#include <linux/tty_flip.h>
> +#include <linux/uaccess.h>
> +#include <linux/workqueue.h>
> +#include <linux/freezer.h>
Gad. Are all those includes needed?
> +#include <asm/system.h>
> +
> +#include <linux/serial_max3100.h>
> +
>
> ...
>
> +/* the following simulate a status reg for ignore_status_mask */
> +#define MAX3100_STATUS_PE 1
> +#define MAX3100_STATUS_FE 2
> +#define MAX3100_STATUS_OE 4
> +
> +struct max3100_port_s {
`struct max3100_port' is sufficient, and would be more typical.
> + struct uart_port port;
> + struct spi_device *spi;
> +
> + int cts:1; /* last CTS received for flow ctrl */
> + int tx_empty:1; /* last TX empty bit */
These two bits will share a word and hence locking is needed to prevent
modifications to one from trashing modifications to the other on SMP.
That's OK, but it would be best to document that locking right here, and
to check that it is adhered to.
> + spinlock_t conf_lock; /* shared data */
> + int conf_commit:1; /* need to make changes */
> + int conf; /* configuration for the MAX31000
> + * (bits 0-7, bits 8-11 are irqs) */
> + int rts_commit:1; /* need to change rts */
> + int rts:1; /* rts status */
> +
> + int parity:3; /* keeps track if we should send parity */
> +#define MAX3100_PARITY_ON 1
> +#define MAX3100_PARITY_ODD 2
> +#define MAX3100_7BIT 4
> + int rx_enabled:1; /* if we should rx chars */
> +
> + int irq; /* irq assigned to the max3100 */
> +
> + int minor; /* minor number */
> + int crystal:1; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
> + int loopback:1; /* 1 if we are in loopback mode */
> + int only_edge_irq:1; /* 1 if we have only edge irqs (like PXA) */
Lots of dittoes.
These bitfields perhaps could be reordered to save a bit of space, but
that depends on the implicit locking rules for them.
> + /* for handling irqs: need workqueue since we do spi_sync */
> + struct workqueue_struct *workqueue;
> + struct work_struct work;
> + /* set to 1 to make the workhandler exit as soon as possible */
> + int force_end_work:1;
> + /* need to know we are suspending to avoid deadlock on workqueue */
> + int suspending:1;
> +
> + /* these are for IRQ on/off counting */
> + int irq_status;
> + /* avoid race condition on irq_status */
> + spinlock_t irq_lock;
> +
> + /* hook for suspending MAX3100 via dedicated pin */
> + void (*max3100_hw_suspend) (int suspend);
> +
> + /* poll time (in ms) for ctrl lines */
> + int poll_time;
> + /* and it's timer */
s/it's/its/
> + struct timer_list timer;
> +};
> +
> +static struct max3100_port_s *max3100s[MAX_MAX3100]; /* the chips */
> +
> +static void irq_state(struct max3100_port_s *s, int on)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(&s->irq_lock, flags);
> + if (s->irq_status != on) {
> + if (on)
> + enable_irq(s->irq);
> + else
> + disable_irq(s->irq);
> + s->irq_status = on;
> + }
> + spin_unlock_irqrestore(&s->irq_lock, flags);
> +}
> +
> +static int max3100_do_parity(struct max3100_port_s *s, u16 c)
> +{
> + int parity;
> + int i, n;
> +
> + if (s->parity & MAX3100_PARITY_ODD)
> + parity = 0;
> + else
> + parity = 1;
> +
> + if (s->parity & MAX3100_7BIT)
> + n = 7;
> + else
> + n = 8;
> +
> + for (i = 0; i < n; i++)
> + parity = parity ^ ((c>>i) & 1);
hrmpf. Don't we have a library function for that?
Perhaps you could use hweight8(c)&1.
> + return parity;
> +}
> +
> +static int max3100_check_parity(struct max3100_port_s *s, u16 c)
> +{
> + return max3100_do_parity(s, c) ==
> + ((c >> (s->parity & MAX3100_7BIT ? 7 : 8)) & 1);
<checks his C precedence table>
OK...
> +}
> +
> +static void max3100_calc_parity(struct max3100_port_s *s, u16 *c)
> +{
> + *c &= ~MAX3100_PT;
s/ / /
> + if (s->parity & MAX3100_PARITY_ON)
> + *c |= max3100_do_parity(s, *c)<<8;
Most people put spaces around <<, but checkpatch pets this through.
> +}
> +
> +static void max3100_work(struct work_struct *w);
> +
> +static void max3100_dowork(struct max3100_port_s *s)
> +{
> + if (!s->force_end_work && !work_pending(&s->work)) {
> + PREPARE_WORK(&s->work, max3100_work);
This is a strange place to run PREPARE_WORK(). Normally it would be
done during driver/device initialsiation, and I think that can be done
here.
> + queue_work(s->workqueue, &s->work);
> + }
> +}
> +
> +static void max3100_timeout(unsigned long data)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *)data;
> +
> + if (s->port.info) {
> + max3100_dowork(s);
> + mod_timer(&s->timer, jiffies + s->poll_time);
> + }
> +}
> +
> +static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx)
> +{
> + struct spi_message message;
> + struct spi_transfer tran;
> + u16 etx, erx;
> + int status;
> +
> + etx = htons(tx);
> + spi_message_init(&message);
> + memset(&tran, 0, sizeof(tran));
> + tran.tx_buf = &etx;
> + tran.rx_buf = &erx;
> + tran.len = 2;
could do
struct spi_transfer tran = {
.tx_buf = &etx,
.rx_buf = &erx
.len = 2,
};
and the compiler will zero out the rest. Minor point.
> + spi_message_add_tail(&tran, &message);
> + status = spi_sync(s->spi, &message);
> + if (status) {
> + dev_warn(&s->spi->dev, "error while calling spi_sync\n");
> + return -EIO;
> + }
> + *rx = ntohs(erx);
> + s->tx_empty = (*rx & MAX3100_T) > 0;
> + dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
> + return 0;
> +}
> +
>
> ...
>
> +static unsigned int max3100_get_mctrl(struct uart_port *port)
> +{
> + struct max3100_port_s *s = container_of(port,
> + struct max3100_port_s,
> + port);
> +
> + dev_dbg(&s->spi->dev, "%s\n", __func__);
> +
> + /* may not be truely up-to-date */
s/truely/truly/
> + max3100_dowork(s);
> + /* always assert DCD and DSR since these lines are not wired */
> + return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
> +}
> +
>
> ...
>
> +static int max3100_startup(struct uart_port *port)
> +{
> + struct max3100_port_s *s = container_of(port,
> + struct max3100_port_s,
> + port);
> + char b[10];
> + int irq_type;
> +
> + dev_dbg(&s->spi->dev, "%s\n", __func__);
> +
> + s->conf = MAX3100_RM;
> + s->rx_enabled = 1;
> +
> + if (s->suspending)
> + return 0;
> +
> + s->force_end_work = 0;
> + s->parity = 0;
> + s->rts = 0;
> +
> + sprintf(b, "max3100-%d", s->minor);
hm. This will go nasty if s->minor > 9. I'd suggest that b[] be made
larger.
> + s->workqueue = create_freezeable_workqueue(b);
> + if (!s->workqueue) {
> + dev_warn(&s->spi->dev, "cannot create workqueue\n");
> + return -EBUSY;
> + }
> + INIT_WORK(&s->work, max3100_work);
> +
> + s->irq_status = 1; /* IRQS are on after request */
> + if (s->only_edge_irq)
> + irq_type = IRQF_TRIGGER_FALLING;
> + else
> + irq_type = IRQF_TRIGGER_LOW;
> + if (request_irq(s->irq, max3100_irq, irq_type, "max3100", s) < 0) {
> + dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
> + s->irq = 0;
Shouldn't we tear down that workqueue before returning?
> + return -EBUSY;
> + }
> +
> + if (s->loopback) {
> + u16 tx, rx;
> + tx = 0x4001;
> + max3100_sr(s, tx, &rx);
> + }
> +
> + if (s->max3100_hw_suspend)
> + s->max3100_hw_suspend(0);
> + s->conf_commit = 1;
> + max3100_dowork(s);
> + /* wait for clock to settle */
> + msleep(50);
> +
> + max3100_enable_ms(&s->port);
> +
> + return 0;
> +}
> +
> +static const char *max3100_type(struct uart_port *port)
> +{
> + struct max3100_port_s *s = container_of(port,
> + struct max3100_port_s,
> + port);
> +
> + dev_dbg(&s->spi->dev, "%s\n", __func__);
> +
> + return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
> +}
> +
> +static void max3100_release_port(struct uart_port *port)
> +{
> + struct max3100_port_s *s = (struct max3100_port_s *)port;
container_of()
> + dev_dbg(&s->spi->dev, "%s\n", __func__);
> +}
> +
>
> ...
>
Please cc Alan Cox for the tty things and perhaps
spi-devel-general@lists.sourceforge.net for the SPI bits.
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH] max3100 driver
@ 2008-09-20 7:20 Christian Pellegrin
2008-09-20 8:24 ` Andrew Morton
2008-09-20 14:11 ` Alan Cox
0 siblings, 2 replies; 21+ messages in thread
From: Christian Pellegrin @ 2008-09-20 7:20 UTC (permalink / raw)
To: linux-kernel; +Cc: linux-serial, Christian Pellegrin
This patch adds support for the MAX3100 SPI UART.
Generated on 20080920 against v2.6.27-rc6
Signed-off-by: Christian Pellegrin <chripell@fsfe.org>
---
drivers/serial/Kconfig | 6 +-
drivers/serial/Makefile | 1 +
drivers/serial/max3100.c | 976 ++++++++++++++++++++++++++++++++++++++++
include/linux/serial_core.h | 3 +
include/linux/serial_max3100.h | 31 ++
5 files changed, 1016 insertions(+), 1 deletions(-)
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 77cb342..de3193d 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -509,7 +509,11 @@ config SERIAL_S3C2440
help
Serial port support for the Samsung S3C2440 and S3C2442 SoC
-
+config SERIAL_MAX3100
+ tristate "MAX3100 support"
+ depends on SPI
+ help
+ MAX3100 chip support
config SERIAL_DZ
bool "DECstation DZ serial driver"
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index 7e7383e..21c3daf 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_SERIAL_S3C2400) += s3c2400.o
obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
obj-$(CONFIG_SERIAL_S3C2412) += s3c2412.o
obj-$(CONFIG_SERIAL_S3C2440) += s3c2440.o
+obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
obj-$(CONFIG_SERIAL_SUNHV) += sunhv.o
obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
new file mode 100644
index 0000000..f8a8af1
--- /dev/null
+++ b/drivers/serial/max3100.c
@@ -0,0 +1,976 @@
+/*
+ *
+ * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ *
+ * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
+ * to use polling for flow control. TX empty IRQ is unusable, since
+ * writing conf clears FIFO buffer and we cannot have this interrupt
+ * always asking us for attention.
+ *
+ * Example platform data:
+
+static struct plat_max3100 max3100_plat_data = {
+ .loopback = 0,
+ .crystal = 0,
+ .only_edge_irq = 0,
+ .poll_time = 100,
+};
+
+static struct spi_board_info spi_board_info[] = {
+ {
+ .modalias = "max3100",
+ .platform_data = &max3100_plat_data,
+ .irq = IRQ_EINT12,
+ .max_speed_hz = 5*1000*1000,
+ .chip_select = 0,
+ },
+};
+
+ * The initial minor number is 128 in the low-density serial port:
+ * mknod /dev/ttyMAX0 c 204 128
+ */
+
+#define MAX3100_MAJOR 204
+#define MAX3100_MINOR 128
+/* 4 MAX3100s should be enough for everyone */
+#define MAX_MAX3100 4
+
+#include <linux/bitops.h>
+#include <linux/console.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/fcntl.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/keyboard.h>
+#include <linux/major.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/sched.h>
+#include <linux/serial_core.h>
+#include <linux/serial.h>
+#include <linux/signal.h>
+#include <linux/spi/spi.h>
+#include <linux/string.h>
+#include <linux/timer.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
+#include <linux/freezer.h>
+
+#include <asm/system.h>
+
+#include <linux/serial_max3100.h>
+
+#define MAX3100_C (1<<14)
+#define MAX3100_D (0<<14)
+#define MAX3100_W (1<<15)
+#define MAX3100_RX (0<<15)
+
+#define MAX3100_WC (MAX3100_W | MAX3100_C)
+#define MAX3100_RC (MAX3100_RX | MAX3100_C)
+#define MAX3100_WD (MAX3100_W | MAX3100_D)
+#define MAX3100_RD (MAX3100_RX | MAX3100_D)
+#define MAX3100_CMD (3 << 14)
+
+#define MAX3100_T (1<<14)
+#define MAX3100_R (1<<15)
+
+#define MAX3100_FEN (1<<13)
+#define MAX3100_SHDN (1<<12)
+#define MAX3100_TM (1<<11)
+#define MAX3100_RM (1<<10)
+#define MAX3100_PM (1<<9)
+#define MAX3100_RAM (1<<8)
+#define MAX3100_IR (1<<7)
+#define MAX3100_ST (1<<6)
+#define MAX3100_PE (1<<5)
+#define MAX3100_L (1<<4)
+#define MAX3100_BAUD (0xf)
+
+#define MAX3100_TE (1<<10)
+#define MAX3100_RAFE (1<<10)
+#define MAX3100_RTS (1<<9)
+#define MAX3100_CTS (1<<9)
+#define MAX3100_PT (1<<8)
+#define MAX3100_DATA (0xff)
+
+#define MAX3100_RT (MAX3100_R | MAX3100_T)
+#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
+
+/* the following simulate a status reg for ignore_status_mask */
+#define MAX3100_STATUS_PE 1
+#define MAX3100_STATUS_FE 2
+#define MAX3100_STATUS_OE 4
+
+struct max3100_port_s {
+ struct uart_port port;
+ struct spi_device *spi;
+
+ int cts:1; /* last CTS received for flow ctrl */
+ int tx_empty:1; /* last TX empty bit */
+
+ spinlock_t conf_lock; /* shared data */
+ int conf_commit:1; /* need to make changes */
+ int conf; /* configuration for the MAX31000
+ * (bits 0-7, bits 8-11 are irqs) */
+ int rts_commit:1; /* need to change rts */
+ int rts:1; /* rts status */
+
+ int parity:3; /* keeps track if we should send parity */
+#define MAX3100_PARITY_ON 1
+#define MAX3100_PARITY_ODD 2
+#define MAX3100_7BIT 4
+ int rx_enabled:1; /* if we should rx chars */
+
+ int irq; /* irq assigned to the max3100 */
+
+ int minor; /* minor number */
+ int crystal:1; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
+ int loopback:1; /* 1 if we are in loopback mode */
+ int only_edge_irq:1; /* 1 if we have only edge irqs (like PXA) */
+
+ /* for handling irqs: need workqueue since we do spi_sync */
+ struct workqueue_struct *workqueue;
+ struct work_struct work;
+ /* set to 1 to make the workhandler exit as soon as possible */
+ int force_end_work:1;
+ /* need to know we are suspending to avoid deadlock on workqueue */
+ int suspending:1;
+
+ /* these are for IRQ on/off counting */
+ int irq_status;
+ /* avoid race condition on irq_status */
+ spinlock_t irq_lock;
+
+ /* hook for suspending MAX3100 via dedicated pin */
+ void (*max3100_hw_suspend) (int suspend);
+
+ /* poll time (in ms) for ctrl lines */
+ int poll_time;
+ /* and it's timer */
+ struct timer_list timer;
+};
+
+static struct max3100_port_s *max3100s[MAX_MAX3100]; /* the chips */
+
+static void irq_state(struct max3100_port_s *s, int on)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->irq_lock, flags);
+ if (s->irq_status != on) {
+ if (on)
+ enable_irq(s->irq);
+ else
+ disable_irq(s->irq);
+ s->irq_status = on;
+ }
+ spin_unlock_irqrestore(&s->irq_lock, flags);
+}
+
+static int max3100_do_parity(struct max3100_port_s *s, u16 c)
+{
+ int parity;
+ int i, n;
+
+ if (s->parity & MAX3100_PARITY_ODD)
+ parity = 0;
+ else
+ parity = 1;
+
+ if (s->parity & MAX3100_7BIT)
+ n = 7;
+ else
+ n = 8;
+
+ for (i = 0; i < n; i++)
+ parity = parity ^ ((c>>i) & 1);
+ return parity;
+}
+
+static int max3100_check_parity(struct max3100_port_s *s, u16 c)
+{
+ return max3100_do_parity(s, c) ==
+ ((c >> (s->parity & MAX3100_7BIT ? 7 : 8)) & 1);
+}
+
+static void max3100_calc_parity(struct max3100_port_s *s, u16 *c)
+{
+ *c &= ~MAX3100_PT;
+ if (s->parity & MAX3100_PARITY_ON)
+ *c |= max3100_do_parity(s, *c)<<8;
+}
+
+static void max3100_work(struct work_struct *w);
+
+static void max3100_dowork(struct max3100_port_s *s)
+{
+ if (!s->force_end_work && !work_pending(&s->work)) {
+ PREPARE_WORK(&s->work, max3100_work);
+ queue_work(s->workqueue, &s->work);
+ }
+}
+
+static void max3100_timeout(unsigned long data)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *)data;
+
+ if (s->port.info) {
+ max3100_dowork(s);
+ mod_timer(&s->timer, jiffies + s->poll_time);
+ }
+}
+
+static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx)
+{
+ struct spi_message message;
+ struct spi_transfer tran;
+ u16 etx, erx;
+ int status;
+
+ etx = htons(tx);
+ spi_message_init(&message);
+ memset(&tran, 0, sizeof(tran));
+ tran.tx_buf = &etx;
+ tran.rx_buf = &erx;
+ tran.len = 2;
+ spi_message_add_tail(&tran, &message);
+ status = spi_sync(s->spi, &message);
+ if (status) {
+ dev_warn(&s->spi->dev, "error while calling spi_sync\n");
+ return -EIO;
+ }
+ *rx = ntohs(erx);
+ s->tx_empty = (*rx & MAX3100_T) > 0;
+ dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
+ return 0;
+}
+
+static int max3100_handlerx(struct max3100_port_s *s, u16 rx)
+{
+ unsigned int ch, flg, status = 0;
+ int ret = 0, cts;
+
+ if (rx & MAX3100_R && s->rx_enabled) {
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+ ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
+ if (rx & MAX3100_RAFE) {
+ s->port.icount.frame++;
+ flg = TTY_FRAME;
+ status |= MAX3100_STATUS_FE;
+ } else {
+ if (s->parity & MAX3100_PARITY_ON) {
+ if (max3100_check_parity(s, rx)) {
+ s->port.icount.rx++;
+ flg = TTY_NORMAL;
+ } else {
+ s->port.icount.parity++;
+ flg = TTY_PARITY;
+ status |= MAX3100_STATUS_PE;
+ }
+ } else {
+ s->port.icount.rx++;
+ flg = TTY_NORMAL;
+ }
+ }
+ uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
+ ret = 1;
+ }
+
+ cts = (rx & MAX3100_CTS) > 0;
+ if (s->cts != cts) {
+ s->cts = cts;
+ uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
+ }
+
+ return ret;
+}
+
+static void max3100_work(struct work_struct *w)
+{
+ struct max3100_port_s *s = container_of(w, struct max3100_port_s, work);
+ int rxchars;
+ u16 tx, rx;
+ int conf, cconf, rts, crts;
+ struct circ_buf *xmit = &s->port.info->xmit;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ do {
+ rxchars = 0;
+
+ spin_lock(&s->conf_lock);
+ conf = s->conf;
+ cconf = s->conf_commit;
+ s->conf_commit = 0;
+ rts = s->rts;
+ crts = s->rts_commit;
+ s->rts_commit = 0;
+ spin_unlock(&s->conf_lock);
+ if (cconf)
+ max3100_sr(s, MAX3100_WC | conf, &rx);
+ if (crts) {
+ max3100_sr(s, MAX3100_WD | MAX3100_TE |
+ (s->rts ? MAX3100_RTS : 0), &rx);
+ rxchars += max3100_handlerx(s, rx);
+ }
+
+ max3100_sr(s, MAX3100_RD, &rx);
+ rxchars += max3100_handlerx(s, rx);
+
+ if (rx & MAX3100_T) {
+ tx = 0xffff;
+ if (s->port.x_char) {
+ tx = s->port.x_char;
+ s->port.icount.tx++;
+ s->port.x_char = 0;
+ } else if (!uart_circ_empty(xmit) &&
+ !uart_tx_stopped(&s->port)) {
+ tx = xmit->buf[xmit->tail];
+ xmit->tail = (xmit->tail + 1) &
+ (UART_XMIT_SIZE - 1);
+ s->port.icount.tx++;
+ }
+ if (tx != 0xffff) {
+ max3100_calc_parity(s, &tx);
+ tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
+ max3100_sr(s, tx, &rx);
+ rxchars += max3100_handlerx(s, rx);
+ }
+ }
+
+ if (rxchars > 0)
+ tty_flip_buffer_push(s->port.info->port.tty);
+ if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
+ uart_write_wakeup(&s->port);
+
+ } while (!s->force_end_work &&
+ !freezing(current) &&
+ ((rx & MAX3100_R) ||
+ (!uart_circ_empty(xmit) &&
+ !uart_tx_stopped(&s->port))));
+
+ if (!s->only_edge_irq && !s->force_end_work &&
+ !freezing(current))
+ irq_state(s, 1);
+}
+
+static irqreturn_t max3100_irq(int irqno, void *dev_id)
+{
+ struct max3100_port_s *s = dev_id;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ if (!s->only_edge_irq)
+ irq_state(s, 0); /* avoid irq storm */
+ max3100_dowork(s);
+ return IRQ_HANDLED;
+}
+
+static void max3100_enable_ms(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ if (s->poll_time > 0)
+ mod_timer(&s->timer, jiffies);
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+}
+
+static void max3100_start_tx(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ max3100_dowork(s);
+}
+
+static void max3100_stop_rx(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ s->rx_enabled = 0;
+ spin_lock(&s->conf_lock);
+ s->conf &= ~MAX3100_RM;
+ s->conf_commit = 1;
+ spin_unlock(&s->conf_lock);
+ max3100_dowork(s);
+}
+
+static unsigned int max3100_tx_empty(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ /* may not be truely up-to-date */
+ max3100_dowork(s);
+ return s->tx_empty;
+}
+
+static unsigned int max3100_get_mctrl(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ /* may not be truely up-to-date */
+ max3100_dowork(s);
+ /* always assert DCD and DSR since these lines are not wired */
+ return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
+}
+
+static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+ int rts;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ rts = (mctrl & TIOCM_RTS) > 0;
+
+ spin_lock(&s->conf_lock);
+ if (s->rts != rts) {
+ s->rts = rts;
+ s->rts_commit = 1;
+ max3100_dowork(s);
+ }
+ spin_unlock(&s->conf_lock);
+}
+
+static void
+max3100_set_termios(struct uart_port *port, struct ktermios *termios,
+ struct ktermios *old)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+ int baud = 0;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ if (!old || (termios->c_cflag != old->c_cflag)) {
+ unsigned cflag;
+ unsigned i;
+ u32 param_new;
+ u32 param_mask;
+ u32 parity = 0;
+
+ cflag = termios->c_cflag;
+ param_new = 0;
+ param_mask = 0;
+
+ i = cflag & CBAUD;
+ switch (i) {
+ case B300:
+ if (s->crystal) {
+ param_new = 16;
+ } else {
+ dev_warn(&s->spi->dev,
+ "unsupported baud rate!\n");
+ param_new = 15;
+ }
+ baud = 300;
+ break;
+ case B600:
+ param_new = 15;
+ baud = 600;
+ break;
+ case B1200:
+ param_new = 14;
+ baud = 1200;
+ break;
+ case B2400:
+ param_new = 13;
+ baud = 2400;
+ break;
+ case B4800:
+ param_new = 12;
+ baud = 4800;
+ break;
+ case B9600:
+ param_new = 11;
+ baud = 9600;
+ break;
+ case B19200:
+ param_new = 10;
+ baud = 19200;
+ break;
+ case B38400:
+ param_new = 9;
+ baud = 38400;
+ break;
+ case B57600:
+ param_new = 2;
+ baud = 57600;
+ break;
+ case B115200:
+ param_new = 1;
+ baud = 11520;
+ break;
+ case B230400:
+ if (s->crystal) {
+ param_new = 1;
+ dev_warn(&s->spi->dev,
+ "unsupported baud rate!\n");
+ } else {
+ param_new = 0;
+ }
+ baud = 230400;
+ break;
+ default:
+ param_new = 1;
+ dev_warn(&s->spi->dev, "invalid baudrate\n");
+ }
+ param_new = (param_new - s->crystal) & MAX3100_BAUD;
+ param_mask |= MAX3100_BAUD;
+
+ if ((cflag & CSIZE) == CS8) {
+ param_new &= ~MAX3100_L;
+ parity &= ~MAX3100_7BIT;
+ } else {
+ param_new |= MAX3100_L;
+ parity |= MAX3100_7BIT;
+ }
+ param_mask |= MAX3100_L;
+
+ if (cflag & CSTOPB)
+ param_new |= MAX3100_ST;
+ else
+ param_new &= ~MAX3100_ST;
+ param_mask |= MAX3100_ST;
+
+ if (cflag & PARENB) {
+ param_new |= MAX3100_PE;
+ parity |= MAX3100_PARITY_ON;
+ } else {
+ param_new &= ~MAX3100_PE;
+ parity &= ~MAX3100_PARITY_ON;
+ }
+ param_mask |= MAX3100_PE;
+
+ if (cflag & PARODD)
+ parity |= MAX3100_PARITY_ODD;
+ else
+ parity &= ~MAX3100_PARITY_ODD;
+
+ s->port.ignore_status_mask = 0;
+ if (termios->c_iflag & IGNPAR)
+ s->port.ignore_status_mask |=
+ MAX3100_STATUS_PE | MAX3100_STATUS_FE |
+ MAX3100_STATUS_OE;
+
+ if (s->poll_time > 0)
+ del_timer_sync(&s->timer);
+
+ uart_update_timeout(port, termios->c_cflag, baud);
+
+ spin_lock(&s->conf_lock);
+ s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
+ s->conf_commit = 1;
+ s->parity = parity;
+ spin_unlock(&s->conf_lock);
+ max3100_dowork(s);
+
+ if (UART_ENABLE_MS(&s->port, termios->c_cflag))
+ max3100_enable_ms(&s->port);
+ }
+}
+
+static void max3100_shutdown(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ if (s->suspending)
+ return;
+
+ s->force_end_work = 1;
+
+ if (s->poll_time > 0)
+ del_timer_sync(&s->timer);
+
+ if (s->workqueue) {
+ flush_workqueue(s->workqueue);
+ destroy_workqueue(s->workqueue);
+ }
+ if (s->irq)
+ free_irq(s->irq, s);
+
+ /* set shutdown mode to save power */
+ if (s->max3100_hw_suspend)
+ s->max3100_hw_suspend(1);
+ else {
+ u16 tx, rx;
+
+ tx = MAX3100_WC | MAX3100_SHDN;
+ max3100_sr(s, tx, &rx);
+ }
+}
+
+static int max3100_startup(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+ char b[10];
+ int irq_type;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ s->conf = MAX3100_RM;
+ s->rx_enabled = 1;
+
+ if (s->suspending)
+ return 0;
+
+ s->force_end_work = 0;
+ s->parity = 0;
+ s->rts = 0;
+
+ sprintf(b, "max3100-%d", s->minor);
+ s->workqueue = create_freezeable_workqueue(b);
+ if (!s->workqueue) {
+ dev_warn(&s->spi->dev, "cannot create workqueue\n");
+ return -EBUSY;
+ }
+ INIT_WORK(&s->work, max3100_work);
+
+ s->irq_status = 1; /* IRQS are on after request */
+ if (s->only_edge_irq)
+ irq_type = IRQF_TRIGGER_FALLING;
+ else
+ irq_type = IRQF_TRIGGER_LOW;
+ if (request_irq(s->irq, max3100_irq, irq_type, "max3100", s) < 0) {
+ dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
+ s->irq = 0;
+ return -EBUSY;
+ }
+
+ if (s->loopback) {
+ u16 tx, rx;
+ tx = 0x4001;
+ max3100_sr(s, tx, &rx);
+ }
+
+ if (s->max3100_hw_suspend)
+ s->max3100_hw_suspend(0);
+ s->conf_commit = 1;
+ max3100_dowork(s);
+ /* wait for clock to settle */
+ msleep(50);
+
+ max3100_enable_ms(&s->port);
+
+ return 0;
+}
+
+static const char *max3100_type(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
+}
+
+static void max3100_release_port(struct uart_port *port)
+{
+ struct max3100_port_s *s = (struct max3100_port_s *)port;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+}
+
+static void max3100_config_port(struct uart_port *port, int flags)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ if (flags & UART_CONFIG_TYPE)
+ s->port.type = PORT_MAX3100;
+}
+
+static int max3100_verify_port(struct uart_port *port,
+ struct serial_struct *ser)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+ int ret = -EINVAL;
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
+ ret = 0;
+ return ret;
+}
+
+static void max3100_stop_tx(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+}
+
+static int max3100_request_port(struct uart_port *port)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+ return 0;
+}
+
+static void max3100_break_ctl(struct uart_port *port, int break_state)
+{
+ struct max3100_port_s *s = container_of(port,
+ struct max3100_port_s,
+ port);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+}
+
+static struct uart_ops max3100_ops = {
+ .tx_empty = max3100_tx_empty,
+ .set_mctrl = max3100_set_mctrl,
+ .get_mctrl = max3100_get_mctrl,
+ .stop_tx = max3100_stop_tx,
+ .start_tx = max3100_start_tx,
+ .stop_rx = max3100_stop_rx,
+ .enable_ms = max3100_enable_ms,
+ .break_ctl = max3100_break_ctl,
+ .startup = max3100_startup,
+ .shutdown = max3100_shutdown,
+ .set_termios = max3100_set_termios,
+ .type = max3100_type,
+ .release_port = max3100_release_port,
+ .request_port = max3100_request_port,
+ .config_port = max3100_config_port,
+ .verify_port = max3100_verify_port,
+};
+
+static struct uart_driver max3100_uart_driver = {
+ .owner = THIS_MODULE,
+ .driver_name = "ttyMAX",
+ .dev_name = "ttyMAX",
+ .major = MAX3100_MAJOR,
+ .minor = MAX3100_MINOR,
+ .nr = MAX_MAX3100,
+};
+static int uart_driver_registered;
+
+static int __devinit max3100_probe(struct spi_device *spi)
+{
+ int i, retval;
+ struct plat_max3100 *pdata;
+ u16 tx, rx;
+
+ if (!uart_driver_registered) {
+ uart_driver_registered = 1;
+ retval = uart_register_driver(&max3100_uart_driver);
+ if (retval) {
+ printk(KERN_ERR "Couldn't register max3100 uart driver\n");
+ return retval;
+ }
+ }
+
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (!max3100s[i])
+ break;
+ if (i == MAX_MAX3100) {
+ dev_warn(&spi->dev, "too many MAX3100 chips\n");
+ return -ENOMEM;
+ }
+
+ max3100s[i] = kzalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
+ if (!max3100s[i]) {
+ dev_warn(&spi->dev,
+ "kmalloc for max3100 structure %d failed!\n", i);
+ return -ENOMEM;
+ }
+ max3100s[i]->spi = spi;
+ max3100s[i]->irq = spi->irq;
+ spin_lock_init(&max3100s[i]->conf_lock);
+ dev_set_drvdata(&spi->dev, max3100s[i]);
+ pdata = spi->dev.platform_data;
+ max3100s[i]->crystal = pdata->crystal;
+ max3100s[i]->loopback = pdata->loopback;
+ max3100s[i]->only_edge_irq = pdata->only_edge_irq;
+ max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
+ if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
+ max3100s[i]->poll_time = 1;
+ max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
+ max3100s[i]->minor = i;
+ init_timer(&max3100s[i]->timer);
+ max3100s[i]->timer.function = max3100_timeout;
+ max3100s[i]->timer.data = (unsigned long) max3100s[i];
+
+ dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
+ max3100s[i]->port.irq = max3100s[i]->irq;
+ max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
+ max3100s[i]->port.fifosize = 16;
+ max3100s[i]->port.ops = &max3100_ops;
+ max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
+ max3100s[i]->port.line = i;
+ max3100s[i]->port.type = PORT_MAX3100;
+ max3100s[i]->port.dev = &spi->dev;
+ retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
+ if (retval < 0)
+ dev_warn(&spi->dev,
+ "uart_add_one_port failed for line %d with error %d\n",
+ i, retval);
+
+ /* set shutdown mode to save power. Will be woken-up on open */
+ if (max3100s[i]->max3100_hw_suspend)
+ max3100s[i]->max3100_hw_suspend(1);
+ else {
+ tx = MAX3100_WC | MAX3100_SHDN;
+ max3100_sr(max3100s[i], tx, &rx);
+ }
+ return 0;
+}
+
+static int __devexit max3100_remove(struct spi_device *spi)
+{
+ struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
+ int i;
+
+ /* find out the index for the chip we are removing */
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (max3100s[i] == s)
+ break;
+
+ dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
+ uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
+ kfree(max3100s[i]);
+ max3100s[i] = NULL;
+
+ /* check if this is the last chip we have */
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (max3100s[i])
+ return 0;
+ pr_debug("removing max3100 driver\n");
+ uart_unregister_driver(&max3100_uart_driver);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM
+
+static int max3100_suspend(struct spi_device *spi, pm_message_t state)
+{
+ struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ s->suspending = 1;
+ uart_suspend_port(&max3100_uart_driver, &s->port);
+
+ irq_state(s, 0);
+ synchronize_irq(s->irq);
+
+ if (s->max3100_hw_suspend)
+ s->max3100_hw_suspend(1);
+ else {
+ /* no HW suspend, so do SW one */
+ u16 tx, rx;
+
+ tx = MAX3100_WC | MAX3100_SHDN;
+ max3100_sr(s, tx, &rx);
+ }
+ return 0;
+}
+
+static int max3100_resume(struct spi_device *spi)
+{
+ struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
+
+ dev_dbg(&s->spi->dev, "%s\n", __func__);
+
+ irq_state(s, 1);
+
+ if (s->max3100_hw_suspend)
+ s->max3100_hw_suspend(0);
+ s->conf_commit = 1;
+ max3100_dowork(s);
+
+ uart_resume_port(&max3100_uart_driver, &s->port);
+ s->suspending = 0;
+
+ return 0;
+}
+
+#else
+#define max3100_suspend NULL
+#define max3100_resume NULL
+#endif
+
+static struct spi_driver max3100_driver = {
+ .driver = {
+ .name = "max3100",
+ .bus = &spi_bus_type,
+ .owner = THIS_MODULE,
+ },
+
+ .probe = max3100_probe,
+ .remove = __devexit_p(max3100_remove),
+ .suspend = max3100_suspend,
+ .resume = max3100_resume,
+};
+
+
+static int __init max3100_init(void)
+{
+ return spi_register_driver(&max3100_driver);
+}
+module_init(max3100_init);
+
+static void __exit max3100_exit(void)
+{
+ spi_unregister_driver(&max3100_driver);
+}
+module_exit(max3100_exit);
+
+MODULE_DESCRIPTION("MAX3100 driver");
+MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 3b2f6c0..32c9fa9 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -155,6 +155,9 @@
#define PORT_SC26XX 82
+/* MAX3100 */
+#define PORT_MAX3100 83
+
#ifdef __KERNEL__
#include <linux/compiler.h>
diff --git a/include/linux/serial_max3100.h b/include/linux/serial_max3100.h
new file mode 100644
index 0000000..d1b4622
--- /dev/null
+++ b/include/linux/serial_max3100.h
@@ -0,0 +1,31 @@
+
+/*
+ *
+ * Copyright (C) 2007 Christian Pellegrin
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#ifndef _LINUX_SERIAL_MAX3100_H
+#define _LINUX_SERIAL_MAX3100_H 1
+
+struct plat_max3100 {
+/* force MAX3100 in loopback */
+ int loopback;
+/* 0 for 3.6864 Mhz, 1 for 1.8432 */
+ int crystal;
+/* for archs like PXA with only edge irqs */
+ int only_edge_irq;
+/* MAX3100 has a shutdown pin. This is a hook
+ called on suspend and resume to activate it.*/
+ void (*max3100_hw_suspend) (int suspend);
+/* poll time for ctr signals in ms, 0 disables (so no hw flow
+ * ctrl is possible) */
+ int poll_time;
+};
+
+#endif
--
1.4.4.4
^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2007-12-17 8:55 ` chri
@ 2007-12-17 9:00 ` Jiri Slaby
0 siblings, 0 replies; 21+ messages in thread
From: Jiri Slaby @ 2007-12-17 9:00 UTC (permalink / raw)
To: chri; +Cc: linux-kernel
On 12/17/2007 09:55 AM, chri wrote:
> On Dec 17, 2007 9:45 AM, Jiri Slaby <jirislaby@gmail.com> wrote:
>> You probably wanted to unregister it after _all_ cards are removed, not after
>> each...
>>
>
> Hi,
>
> thanks you are right. I haven't noticed this since spi devices are not
> hot-pluggable so I could remove them only with rmmod (and so all of
> them toghether). Anyway the code I sent is ugly, I will correct and
> resend.
Won't bind/unbind in sysfs help you?
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2007-12-17 8:45 ` Jiri Slaby
@ 2007-12-17 8:55 ` chri
2007-12-17 9:00 ` Jiri Slaby
0 siblings, 1 reply; 21+ messages in thread
From: chri @ 2007-12-17 8:55 UTC (permalink / raw)
To: Jiri Slaby; +Cc: linux-kernel
On Dec 17, 2007 9:45 AM, Jiri Slaby <jirislaby@gmail.com> wrote:
>
> You probably wanted to unregister it after _all_ cards are removed, not after
> each...
>
Hi,
thanks you are right. I haven't noticed this since spi devices are not
hot-pluggable so I could remove them only with rmmod (and so all of
them toghether). Anyway the code I sent is ugly, I will correct and
resend.
>
> Also you should synchronize_irq() after stopping them here.
>
I see. I guess I will have problems on suspending with serial activity
otherwise.
Thanks,
--
Christian Pellegrin, see http://www.evolware.org/chri/
"Real Programmers don't play tennis, or any other sport which requires
you to change clothes. Mountain climbing is OK, and Real Programmers
wear their climbing boots to work in case a mountain should suddenly
spring up in the middle of the computer room."
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH] max3100 driver
2007-12-17 8:17 chripell
@ 2007-12-17 8:45 ` Jiri Slaby
2007-12-17 8:55 ` chri
0 siblings, 1 reply; 21+ messages in thread
From: Jiri Slaby @ 2007-12-17 8:45 UTC (permalink / raw)
To: chripell; +Cc: linux-kernel, Christian Pellegrin
On 12/17/2007 09:17 AM, chripell@gmail.com wrote:
> This patch adds support for the MAX3100 SPI UART.
>
> Generated on 20071217 against v2.6.23
>
> Signed-off-by: Christian Pellegrin <chripell@fsfe.org>
> ---
> drivers/serial/Kconfig | 7 +
> drivers/serial/Makefile | 1 +
> drivers/serial/max3100.c | 956 ++++++++++++++++++++++++++++++++++++++++
> include/linux/serial_max3100.h | 25 +
> 4 files changed, 989 insertions(+), 0 deletions(-)
>
[...]
> diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
> new file mode 100644
> index 0000000..d07936d
> --- /dev/null
> +++ b/drivers/serial/max3100.c
[...]
> +static int __devexit max3100_remove(struct spi_device *spi)
> +{
> + int i;
> +
> + i = 0;
> +
> + for (i = 0; i < MAX_MAX3100; i++)
> + if (max3100s[i] && max3100s[i]->ref_count > 0)
> + return -EBUSY;
> +
> + for (i = 0; i < MAX_MAX3100; i++)
> + if (max3100s[i]) {
> + tty_unregister_device(serial_driver, i);
> + kfree(max3100s[i]);
> + max3100s[i] = NULL;
> + }
> +
> + if (serial_driver)
> + tty_unregister_driver(serial_driver);
You probably wanted to unregister it after _all_ cards are removed, not after
each...
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int max3100_suspend(struct spi_device *spi, pm_message_t state)
> +{
> + struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
> + u16 tx, rx;
> +
> + tx = MAX3100_WC | MAX3100_SHDN;
> + max3100_sr(s, tx, &rx, 0);
Also you should synchronize_irq() after stopping them here.
> + return 0;
> +}
regards,
--
Jiri Slaby (jirislaby@gmail.com)
Faculty of Informatics, Masaryk University
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH] max3100 driver
@ 2007-12-17 8:17 chripell
2007-12-17 8:45 ` Jiri Slaby
0 siblings, 1 reply; 21+ messages in thread
From: chripell @ 2007-12-17 8:17 UTC (permalink / raw)
To: linux-kernel; +Cc: Christian Pellegrin
This patch adds support for the MAX3100 SPI UART.
Generated on 20071217 against v2.6.23
Signed-off-by: Christian Pellegrin <chripell@fsfe.org>
---
drivers/serial/Kconfig | 7 +
drivers/serial/Makefile | 1 +
drivers/serial/max3100.c | 956 ++++++++++++++++++++++++++++++++++++++++
include/linux/serial_max3100.h | 25 +
4 files changed, 989 insertions(+), 0 deletions(-)
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 81b52b7..4e7111b 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -461,6 +461,13 @@ config SERIAL_S3C2410_CONSOLE
your boot loader about how to pass options to the kernel at
boot time.)
+config SERIAL_MAX3100
+ tristate "MAX3100 support"
+ depends on SPI
+ help
+ MAX3100 chip support
+
+
config SERIAL_DZ
bool "DECstation DZ serial driver"
depends on MACH_DECSTATION && 32BIT
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index af6377d..9f67e52 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_SERIAL_PNX8XXX) += pnx8xxx_uart.o
obj-$(CONFIG_SERIAL_SA1100) += sa1100.o
obj-$(CONFIG_SERIAL_BFIN) += bfin_5xx.o
obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
+obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
obj-$(CONFIG_SERIAL_SUNHV) += sunhv.o
obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
diff --git a/drivers/serial/max3100.c b/drivers/serial/max3100.c
new file mode 100644
index 0000000..d07936d
--- /dev/null
+++ b/drivers/serial/max3100.c
@@ -0,0 +1,956 @@
+
+/*
+ *
+ * Copyright (C) 2007 Christian Pellegrin <chripell@evolware.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ *
+ * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
+ * to use polling for flow control. TX empty IRQ is unusable, since
+ * writing conf clears FIFO buffer and we cannot have this interrupt
+ * always asking us for attention.
+ *
+ * Example platform data:
+
+static struct plat_max3100 max3100_plat_data = {
+ .loopback = 0,
+ .crystal = 0,
+ .only_edge_irq = 0,
+};
+
+static struct spi_board_info spi_board_info[] = {
+ {
+ .modalias = "max3100",
+ .platform_data = &max3100_plat_data,
+ .irq = IRQ_EINT12,
+ .max_speed_hz = 5*1000*1000,
+ .chip_select = 0,
+ },
+};
+
+ * The initial minor number is 128 to prevent clashes with ttyS:
+ * mknod /dev/ttyMAX0 c 4 128
+ */
+
+#include <linux/bitops.h>
+#include <linux/console.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/fcntl.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/keyboard.h>
+#include <linux/major.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/reboot.h>
+#include <linux/sched.h>
+#include <linux/serial_core.h>
+#include <linux/signal.h>
+#include <linux/spi/spi.h>
+#include <linux/string.h>
+#include <linux/timer.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
+#include <linux/uaccess.h>
+#include <linux/workqueue.h>
+
+#include <asm/system.h>
+
+#include <linux/serial_max3100.h>
+
+#define MAX3100_C (1<<14)
+#define MAX3100_D (0<<14)
+#define MAX3100_W (1<<15)
+#define MAX3100_RX (0<<15)
+
+#define MAX3100_WC (MAX3100_W | MAX3100_C)
+#define MAX3100_RC (MAX3100_RX | MAX3100_C)
+#define MAX3100_WD (MAX3100_W | MAX3100_D)
+#define MAX3100_RD (MAX3100_RX | MAX3100_D)
+#define MAX3100_CMD (3 << 14)
+
+#define MAX3100_T (1<<14)
+#define MAX3100_R (1<<15)
+
+#define MAX3100_FEN (1<<13)
+#define MAX3100_SHDN (1<<12)
+#define MAX3100_TM (1<<11)
+#define MAX3100_RM (1<<10)
+#define MAX3100_PM (1<<9)
+#define MAX3100_RAM (1<<8)
+#define MAX3100_IR (1<<7)
+#define MAX3100_ST (1<<6)
+#define MAX3100_PE (1<<5)
+#define MAX3100_L (1<<4)
+#define MAX3100_BAUD (0xf)
+
+#define MAX3100_TE (1<<10)
+#define MAX3100_RAFE (1<<10)
+#define MAX3100_RTS (1<<9)
+#define MAX3100_CTS (1<<9)
+#define MAX3100_PT (1<<8)
+#define MAX3100_DATA (0xff)
+
+#define MAX3100_RT (MAX3100_R | MAX3100_T)
+#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
+
+struct max3100_port_s {
+ struct uart_port port;
+ struct spi_device *spi;
+ struct tty_struct *tty;
+
+ struct mutex spi_txrx;/* protects access to the hw */
+
+ int rts; /* rts status, can be MAX3100_RTS or 0 */
+ int conf; /* configuration for the MAX31000
+ * (bits 0-7, bits 8-11 are irqs) */
+ int last_cts_rx; /* last CTS received for flow ctrl */
+
+ int tx_buf_cur; /* current char to tx */
+ int tx_buf_tot; /* current number of chars in tx buf */
+ unsigned char *tx_buf;
+ /* shared tx buffer spinlock (no sem
+ * since write may sleep) */
+ spinlock_t tx_buf_lock;
+
+ int tx_stopped; /* when we should not send chars */
+ int parity; /* keeps track if we should work parity */
+#define MAX3100_PARITY_ON 1
+#define MAX3100_PARITY_ODD 2
+#define MAX3100_7BIT 4
+
+ int irq; /* irq assigned to the max3100 */
+
+ int minor; /* minor number */
+ int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
+ int loopback; /* 1 if we are in loopback mode */
+ int only_edge_irq; /* 1 if we have only edge irqs (like PXA) */
+
+ int ref_count; /* how many users */
+ struct mutex sem; /* protects us during open/close */
+
+ /* for handling irqs: need workqueue since we do spi_sync */
+ struct workqueue_struct *workqueue;
+ struct work_struct work;
+ /* set to 1 to make the workhandler exit as soon as possible */
+ int force_end_work;
+
+ /* these are for IRQ on/off counting */
+ int irq_status;
+ /* avoid race condition on irq_status */
+ spinlock_t irq_lock;
+
+ /* signals when we done sending current buffer */
+ wait_queue_head_t all_sent;
+};
+
+/* global since we do register the driver only once */
+static struct tty_driver *serial_driver;
+
+/* 4 MAX3100s should be enough for everyone */
+#define MAX_MAX3100 4
+static struct max3100_port_s *max3100s[MAX_MAX3100]; /* the chips */
+
+/* our buffer for sending chars */
+#define MAX3100_TX_BUF_N 16
+
+/* how many chars we wait befor flipping tty buffer */
+#define MAX3100_FLIP_EVERY 8
+
+static void irq_state(struct max3100_port_s *s, int on)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->irq_lock, flags);
+ if (s->irq_status != on) {
+ if (on)
+ enable_irq(s->irq);
+ else
+ disable_irq(s->irq);
+ s->irq_status = on;
+ }
+ spin_unlock_irqrestore(&s->irq_lock, flags);
+}
+
+
+static int max3100_do_parity(struct max3100_port_s *s, u16 c)
+{
+ int parity;
+ int i, n;
+
+ if (s->parity & MAX3100_PARITY_ODD)
+ parity = 0;
+ else
+ parity = 1;
+
+ if (s->parity & MAX3100_7BIT)
+ n = 7;
+ else
+ n = 8;
+
+ for (i = 0; i < n; i++)
+ parity = parity ^ ((c>>i) & 1);
+ return parity;
+}
+
+static int max3100_check_parity(struct max3100_port_s *s, u16 c)
+{
+ return max3100_do_parity(s, c) == ((c>>9) & 1);
+}
+
+static void max3100_calc_parity(struct max3100_port_s *s, u16 *c)
+{
+ *c &= ~MAX3100_PT;
+ *c |= max3100_do_parity(s, *c)<<8;
+}
+
+
+static void max3100_work(struct work_struct *w);
+
+static void max3100_dowork(struct max3100_port_s *s)
+{
+ if (!s->force_end_work) {
+ PREPARE_WORK(&s->work, max3100_work);
+ queue_work(s->workqueue, &s->work);
+ }
+}
+
+static int max3100_sr(struct max3100_port_s *s, u16 tx, u16 *rx, int push)
+{
+ /* note: we suppose that the T bit from conf_status is in sync
+ * with the hw one, so all the communication must go through
+ * this function */
+ struct spi_message message;
+ struct spi_transfer tran;
+ int status, flag;
+ u16 etx, erx;
+ int got_char = 0;
+
+ mutex_lock(&s->spi_txrx);
+
+ if (s->loopback) {
+ if ((tx & MAX3100_CMD) == MAX3100_RC)
+ tx |= 1;
+ }
+ etx = htons(tx);
+ spi_message_init(&message);
+ memset(&tran, 0, sizeof(tran));
+ tran.tx_buf = &etx;
+ tran.rx_buf = &erx;
+ tran.len = 2;
+ spi_message_add_tail(&tran, &message);
+ status = spi_sync(s->spi, &message);
+ if (status) {
+ dev_warn(&s->spi->dev, "error while calling spi_sync\n");
+ return 0;
+ }
+ *rx = ntohs(erx);
+ dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
+
+ if ((tx & MAX3100_CMD) == MAX3100_RD ||
+ (tx & MAX3100_CMD) == MAX3100_WD)
+ s->last_cts_rx = *rx;
+
+ if (*rx & MAX3100_R &&
+ ((tx & MAX3100_CMD) == MAX3100_RD ||
+ (tx & MAX3100_CMD) == MAX3100_WD)) {
+ got_char = 1;
+ if (tty_buffer_request_room(s->tty, 1) == 0) {
+ dev_warn(&s->spi->dev, "no room in tty buffer\n");
+ tty_schedule_flip(s->tty);
+ }
+
+ flag = TTY_NORMAL;
+ if (*rx & MAX3100_RAFE) {
+ s->port.icount.frame++;
+ flag = TTY_FRAME;
+ }
+ if (s->parity & MAX3100_PARITY_ON &&
+ max3100_check_parity(s, *rx)) {
+ s->port.icount.parity++;
+ flag = TTY_PARITY;
+ }
+ tty_insert_flip_char(s->tty, *rx & MAX3100_DATA, flag);
+ }
+
+ mutex_unlock(&s->spi_txrx);
+
+ if (push && got_char)
+ tty_schedule_flip(s->tty);
+ return got_char;
+}
+
+
+static void max3100_send_conf(struct max3100_port_s *s)
+{
+ u16 tx = 0, rx;
+
+ tx = MAX3100_WC | (s->conf & 0x0fff);
+ max3100_sr(s, tx, &rx, 1);
+}
+
+static int max3100_ok_to_send(struct max3100_port_s *s)
+{
+ if (C_CRTSCTS(s->tty)) {
+ if ((s->last_cts_rx & MAX3100_CTS) == 0)
+ return 0;
+ }
+ return !s->tx_stopped;
+}
+
+static void max3100_work(struct work_struct *w)
+{
+ struct max3100_port_s *s = container_of(w, struct max3100_port_s, work);
+ u16 tx, rx;
+ int nchars; /* number of char waiting in the send buf */
+ int rxchars = 0; /* chars received */
+
+ do {
+ unsigned long flags;
+
+ tx = MAX3100_RD;
+ rxchars += max3100_sr(s, tx, &rx, 0);
+ /* rx a char so we get MAX3100_T and CTS current */
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ nchars = s->tx_buf_tot - s->tx_buf_cur;
+ tx = MAX3100_WD | s->rts;
+ tx |= s->tx_buf[s->tx_buf_cur];
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+
+ if ((rx & MAX3100_T) && /* tx buffer empty*/
+ max3100_ok_to_send(s) && /* fw ctrl decision */
+ nchars > 0 && /* more to send */
+ !s->force_end_work) {
+ if (s->parity & MAX3100_PARITY_ON)
+ max3100_calc_parity(s, &tx);
+ rxchars += max3100_sr(s, tx, &rx, 0);
+ nchars -= 1;
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ s->tx_buf_cur += 1;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ if (nchars == 0) {
+ tty_wakeup(s->tty);
+ wake_up(&s->all_sent);
+ }
+ }
+ if (rxchars > MAX3100_FLIP_EVERY) {
+ tty_schedule_flip(s->tty);
+ rxchars = 0;
+ }
+ }
+ while ((nchars > 0 || /* more to send */
+ rx & MAX3100_R) && /* try to empy all the RX buffer */
+ !s->force_end_work);
+ if (rxchars)
+ tty_schedule_flip(s->tty);
+ if (!s->only_edge_irq && !s->force_end_work)
+ irq_state(s, 1);
+}
+
+static irqreturn_t max3100_irq(int irqno, void *dev_id)
+{
+ struct max3100_port_s *s = dev_id;
+
+ if (!s->only_edge_irq)
+ irq_state(s, 0); /* avoid irq storm */
+ max3100_dowork(s);
+ return IRQ_HANDLED;
+}
+
+static void rs_stop(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ s->tx_stopped = 1;
+}
+
+static void rs_start(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ s->tx_stopped = 0;
+}
+
+static void rs_flush_chars(struct tty_struct *tty)
+{
+}
+
+static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ wait_event_timeout(s->all_sent,
+ s->tx_buf_tot == 0 || s->tx_buf_tot == s->tx_buf_cur,
+ timeout);
+}
+
+static void rs_flush_buffer(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = 0;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+}
+
+static int rs_write(struct tty_struct *tty,
+ const unsigned char *buf, int count)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot) {
+ count = 0;
+ } else {
+ if (count > MAX3100_TX_BUF_N)
+ count = MAX3100_TX_BUF_N;
+ memcpy(s->tx_buf, buf, count);
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = count;
+ }
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ max3100_dowork(s);
+ return count;
+}
+
+static int rs_write_room(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ int ret;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot)
+ ret = 0;
+ else
+ ret = MAX3100_TX_BUF_N;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ return ret;
+}
+
+static int rs_chars_in_buffer(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ int ret;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->tx_buf_lock, flags);
+ if (s->tx_buf_cur < s->tx_buf_tot)
+ ret = MAX3100_TX_BUF_N;
+ else
+ ret = 0;
+ spin_unlock_irqrestore(&s->tx_buf_lock, flags);
+ return ret;
+}
+
+static void internal_throttle(struct tty_struct *tty, char ch, int rts)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ u16 tx = 0, rx;
+ int old_tx_stopped;
+
+ old_tx_stopped = s->tx_stopped;
+ s->tx_stopped = 1;
+ tx = MAX3100_RD;
+ /* wait for tx buf empty. Unfortunately we cannot avoid a busy
+ loop (it should not take much since we block tx ... so at
+ most a byte time) since TX empty interrupt is unusable (see
+ Notes on top) */
+ do {
+ max3100_sr(s, tx, &rx, 1);
+ schedule();
+ } while ((rx & MAX3100_T) == 0);
+
+ if (I_IXOFF(tty))
+ tx = MAX3100_WD | s->rts | (ch&0xff);
+ if (C_CRTSCTS(tty)) {
+ s->rts = rts ? (MAX3100_RTS) : 0;
+ tx = MAX3100_WD | s->rts | MAX3100_TE;
+ }
+ if (tx)
+ max3100_sr(s, tx, &rx, 1);
+ s->tx_stopped = old_tx_stopped;
+}
+
+static void rs_throttle(struct tty_struct *tty)
+{
+ internal_throttle(tty, STOP_CHAR(tty), 0);
+}
+
+static void rs_unthrottle(struct tty_struct *tty)
+{
+ internal_throttle(tty, START_CHAR(tty), 1);
+}
+
+static int get_lsr_info(struct max3100_port_s *s, unsigned int *value)
+{
+ unsigned char status;
+ u16 tx = 0, rx;
+
+ tx = MAX3100_RD;
+ max3100_sr(s, tx, &rx, 1);
+ status = (rx & MAX3100_CTS) > 0;
+ return put_user(status, value);
+}
+
+static int rs_ioctl(struct tty_struct *tty, struct file *file,
+ unsigned int cmd, unsigned long arg)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ int retval = 0;
+
+ switch (cmd) {
+
+ case TIOCSERGETLSR: /* Get line status register */
+ return get_lsr_info(s, (unsigned int *) arg);
+
+ default:
+ retval = -ENOIOCTLCMD ;
+ }
+ return retval;
+}
+
+static void change_speed(struct max3100_port_s *s)
+{
+ unsigned cflag;
+ unsigned i;
+ u32 param_new;
+ u32 param_mask;
+
+ cflag = s->tty->termios->c_cflag;
+ param_new = 0;
+ param_mask = 0;
+
+ i = cflag & CBAUD;
+ /* note: supposed 3.6864 Mhz xtal */
+ switch (i) {
+ case B300:
+ if (s->crystal) {
+ param_new = 16;
+ } else {
+ dev_warn(&s->spi->dev, "unsupported baud rate!\n");
+ param_new = 15;
+ }
+ break;
+ case B600:
+ param_new = 15;
+ break;
+ case B1200:
+ param_new = 14;
+ break;
+ case B2400:
+ param_new = 13;
+ break;
+ case B4800:
+ param_new = 12;
+ break;
+ case B9600:
+ param_new = 11;
+ break;
+ case B19200:
+ param_new = 10;
+ break;
+ case B38400:
+ param_new = 9;
+ break;
+ case B57600:
+ param_new = 2;
+ break;
+ case B115200:
+ param_new = 1;
+ break;
+ case B230400:
+ if (s->crystal) {
+ param_new = 1;
+ dev_warn(&s->spi->dev, "unsupported baud rate!\n");
+ } else {
+ param_new = 0;
+ }
+ break;
+ default:
+ param_new = 1;
+ dev_warn(&s->spi->dev, "invalid baudrate\n");
+ }
+ param_new = (param_new - s->crystal) & MAX3100_BAUD;
+ param_mask |= MAX3100_BAUD;
+
+ if ((cflag & CSIZE) == CS8) {
+ param_new &= ~MAX3100_L;
+ s->parity &= ~MAX3100_7BIT;
+ } else {
+ param_new |= MAX3100_L;
+ s->parity |= MAX3100_7BIT;
+ }
+ param_mask |= MAX3100_L;
+
+ if (cflag & CSTOPB)
+ param_new |= MAX3100_ST;
+ else
+ param_new &= ~MAX3100_ST;
+ param_mask |= MAX3100_ST;
+
+ if (cflag & PARENB) {
+ param_new |= MAX3100_PE;
+ s->parity |= MAX3100_PARITY_ON;
+ } else {
+ param_new &= ~MAX3100_PE;
+ s->parity &= ~MAX3100_PARITY_ON;
+ }
+ param_mask |= MAX3100_PE;
+
+ if (cflag & PARODD) {
+ s->parity |= MAX3100_PARITY_ODD;
+ } else {
+ s->parity &= ~MAX3100_PARITY_ODD;
+ }
+
+ s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
+ max3100_send_conf(s);
+
+ return;
+}
+
+
+static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ if (tty->termios->c_cflag == old_termios->c_cflag)
+ return;
+
+ change_speed(s);
+
+ if ((old_termios->c_cflag & CRTSCTS) &&
+ !(tty->termios->c_cflag & CRTSCTS))
+ max3100_dowork(s);
+}
+
+static void shutdown(struct max3100_port_s *s)
+{
+ s->force_end_work = 1;
+ if (s->workqueue) {
+ flush_workqueue(s->workqueue);
+ destroy_workqueue(s->workqueue);
+ }
+ if (s->irq)
+ free_irq(s->irq, s);
+ kfree(s->tx_buf);
+ s->tx_buf = NULL;
+ if (s->tty)
+ set_bit(TTY_IO_ERROR, &s->tty->flags);
+}
+
+static void rs_hangup(struct tty_struct *tty)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ shutdown(s);
+}
+
+static int startup(struct max3100_port_s *s)
+{
+ char b[10];
+ int irq_type;
+
+ s->tx_buf = kmalloc(MAX3100_TX_BUF_N, GFP_KERNEL);
+ if (s->tx_buf == NULL)
+ return -ENOMEM;
+ s->tx_buf_cur = 0;
+ s->tx_buf_tot = 0;
+
+ s->force_end_work = 0;
+ s->parity = 0;
+ s->rts = 0;
+ s->tx_stopped = 0;
+ s->conf = MAX3100_RM;
+
+ sprintf(b, "max3100-%d", s->minor);
+ s->workqueue = create_singlethread_workqueue(b);
+ if (!s->workqueue) {
+ dev_warn(&s->spi->dev, "cannot create workqueue\n");
+ return -EBUSY;
+ }
+ INIT_WORK(&s->work, max3100_work);
+
+ s->irq_status = 1; /* IRQS are on after request */
+ if (s->only_edge_irq)
+ irq_type = IRQF_TRIGGER_FALLING;
+ else
+ irq_type = IRQF_TRIGGER_LOW;
+ if (request_irq(s->irq, max3100_irq, irq_type, "max3100", s) < 0) {
+ dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
+ s->irq = 0;
+ kfree(s->tx_buf);
+ return -EBUSY;
+ }
+
+ if (s->tty)
+ clear_bit(TTY_IO_ERROR, &s->tty->flags);
+
+ change_speed(s);
+
+ if (s->loopback) {
+ u16 tx, rx;
+ tx = 0x4001;
+ max3100_sr(s, tx, &rx, 1);
+ }
+
+ return 0;
+}
+
+int rs_open(struct tty_struct *tty, struct file *filp)
+{
+ struct max3100_port_s *s;
+ int retval = 0, line;
+
+ line = tty->index;
+
+ if (line >= MAX_MAX3100 || line < 0)
+ return -ENODEV;
+
+ s = max3100s[line];
+
+ if (!s) {
+ printk(KERN_ERR "Nonexistent max3100 for index %d\n", line);
+ return -ENODEV;
+ }
+
+ retval = mutex_lock_interruptible(&s->sem);
+ if (retval < 0)
+ return retval;
+
+ s->ref_count++;
+
+ if (s->ref_count == 1) {
+ tty->driver_data = s;
+ s->tty = tty;
+
+ retval = startup(s);
+ }
+
+ mutex_unlock(&s->sem);
+
+ return retval;
+}
+
+static void rs_close(struct tty_struct *tty, struct file *filp)
+{
+ struct max3100_port_s *s = tty->driver_data;
+
+ mutex_lock(&s->sem);
+
+ s->ref_count--;
+ if (s->ref_count == 0)
+ shutdown(s);
+
+ mutex_unlock(&s->sem);
+}
+
+static int rs_tiocmget(struct tty_struct *tty, struct file *file)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ u16 tx, rx;
+ unsigned int result;
+
+ tx = MAX3100_RD;
+ max3100_sr(s, tx, &rx, 1);
+
+ result = ((s->rts) ? TIOCM_RTS : 0)
+ | ((rx & MAX3100_CTS) ? TIOCM_CTS : 0);
+ return result;
+}
+
+static int rs_tiocmset(struct tty_struct *tty, struct file *file,
+ unsigned int set, unsigned int clear)
+{
+ struct max3100_port_s *s = tty->driver_data;
+ int old_rts = s->rts;
+ u16 tx, rx;
+
+ if (set & TIOCM_RTS)
+ s->rts = MAX3100_RTS;
+ if (clear & TIOCM_RTS)
+ s->rts = 0;
+
+ if (s->rts != old_rts) {
+ tx = MAX3100_WD | s->rts | MAX3100_TE;
+ max3100_sr(s, tx, &rx, 1);
+ }
+ return 0;
+}
+
+static struct tty_operations rs_ops = {
+ .open = rs_open,
+ .close = rs_close,
+ .write = rs_write,
+ .flush_chars = rs_flush_chars,
+ .wait_until_sent = rs_wait_until_sent,
+ .write_room = rs_write_room,
+ .chars_in_buffer = rs_chars_in_buffer,
+ .flush_buffer = rs_flush_buffer,
+ .ioctl = rs_ioctl,
+ .throttle = rs_throttle,
+ .unthrottle = rs_unthrottle,
+ .set_termios = rs_set_termios,
+ .stop = rs_stop,
+ .start = rs_start,
+ .hangup = rs_hangup,
+ .tiocmget = rs_tiocmget,
+ .tiocmset = rs_tiocmset,
+};
+
+static int __devinit max3100_probe(struct spi_device *spi)
+{
+ int i;
+ struct plat_max3100 *pdata;
+
+ if (!serial_driver) {
+ serial_driver = alloc_tty_driver(MAX_MAX3100);
+ if (!serial_driver) {
+ printk(KERN_ERR "Cannot allocate serial driver\n");
+ return -ENOMEM;
+ }
+
+ serial_driver->name = "ttyMAX";
+ serial_driver->driver_name = "ttyMAX";
+ serial_driver->major = TTY_MAJOR;
+ /* this should prevent clashes */
+ serial_driver->minor_start = 128;
+ serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
+ serial_driver->subtype = SERIAL_TYPE_NORMAL;
+ serial_driver->init_termios = tty_std_termios;
+ serial_driver->init_termios.c_cflag =
+ B9600 | CS8 | CREAD | HUPCL | CLOCAL;
+ serial_driver->flags = TTY_DRIVER_REAL_RAW |
+ TTY_DRIVER_DYNAMIC_DEV;
+ tty_set_operations(serial_driver, &rs_ops);
+
+ if (tty_register_driver(serial_driver)) {
+ put_tty_driver(serial_driver);
+ printk(KERN_ERR "Couldn't register serial driver\n");
+ return -EINVAL;
+ }
+ }
+
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (!max3100s[i])
+ break;
+ if (i == MAX_MAX3100) {
+ dev_warn(&spi->dev, "too many MAX3100 chips\n");
+ return -ENOMEM;
+ }
+
+ max3100s[i] = kzalloc(sizeof(struct max3100_port_s), GFP_KERNEL);
+ if (!max3100s[i]) {
+ dev_warn(&spi->dev,
+ "kmalloc for max3100 structure %d failed!\n", i);
+ return -ENOMEM;
+ }
+ max3100s[i]->spi = spi;
+ max3100s[i]->irq = spi->irq;
+ mutex_init(&max3100s[i]->sem);
+ mutex_init(&max3100s[i]->spi_txrx);
+ spin_lock_init(&max3100s[i]->tx_buf_lock);
+ spin_lock_init(&max3100s[i]->irq_lock);
+ init_waitqueue_head(&max3100s[i]->all_sent);
+ dev_set_drvdata(&spi->dev, &max3100s[i]);
+ pdata = spi->dev.platform_data;
+ max3100s[i]->crystal = pdata->crystal;
+ max3100s[i]->loopback = pdata->loopback;
+ max3100s[i]->only_edge_irq = pdata->only_edge_irq;
+ max3100s[i]->minor = i;
+ tty_register_device(serial_driver, i, &spi->dev);
+
+ return 0;
+}
+
+static int __devexit max3100_remove(struct spi_device *spi)
+{
+ int i;
+
+ i = 0;
+
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (max3100s[i] && max3100s[i]->ref_count > 0)
+ return -EBUSY;
+
+ for (i = 0; i < MAX_MAX3100; i++)
+ if (max3100s[i]) {
+ tty_unregister_device(serial_driver, i);
+ kfree(max3100s[i]);
+ max3100s[i] = NULL;
+ }
+
+ if (serial_driver)
+ tty_unregister_driver(serial_driver);
+ return 0;
+}
+
+#ifdef CONFIG_PM
+static int max3100_suspend(struct spi_device *spi, pm_message_t state)
+{
+ struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
+ u16 tx, rx;
+
+ tx = MAX3100_WC | MAX3100_SHDN;
+ max3100_sr(s, tx, &rx, 0);
+ return 0;
+}
+
+static int max3100_resume(struct spi_device *spi)
+{
+ struct max3100_port_s *s = dev_get_drvdata(&spi->dev);
+
+ max3100_send_conf(s);
+
+ return 0;
+}
+
+#else
+#define max3100_suspend NULL
+#define max3100_resume NULL
+#endif
+
+static struct spi_driver max3100_driver = {
+ .driver = {
+ .name = "max3100",
+ .bus = &spi_bus_type,
+ .owner = THIS_MODULE,
+ },
+
+ .probe = max3100_probe,
+ .remove = __devexit_p(max3100_remove),
+ .suspend = max3100_suspend,
+ .resume = max3100_resume,
+};
+
+
+static int __init max3100_init(void)
+{
+ return spi_register_driver(&max3100_driver);
+}
+module_init(max3100_init);
+
+static void __exit max3100_exit(void)
+{
+ spi_unregister_driver(&max3100_driver);
+}
+module_exit(max3100_exit);
+
+MODULE_DESCRIPTION("MAX3100 driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/serial_max3100.h b/include/linux/serial_max3100.h
new file mode 100644
index 0000000..c6b5c45
--- /dev/null
+++ b/include/linux/serial_max3100.h
@@ -0,0 +1,25 @@
+
+/*
+ *
+ * Copyright (C) 2007 Christian Pellegrin
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#ifndef _LINUX_SERIAL_MAX3100_H
+#define _LINUX_SERIAL_MAX3100_H 1
+
+struct plat_max3100 {
+ int loopback;
+/* force MAX3100 in loopback */
+ int crystal;
+/* 0 for 3.6864 Mhz, 1 for 1.8432 */
+ int only_edge_irq;
+/* for archs like PXA with only edge irqs */
+};
+
+#endif
--
1.4.4.4
^ permalink raw reply related [flat|nested] 21+ messages in thread
end of thread, other threads:[~2008-10-10 12:09 UTC | newest]
Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-05 10:26 [PATCH] max3100 driver chripell
2007-12-12 9:49 ` Andrew Morton
2007-12-12 11:58 ` chri
2007-12-17 8:17 chripell
2007-12-17 8:45 ` Jiri Slaby
2007-12-17 8:55 ` chri
2007-12-17 9:00 ` Jiri Slaby
2008-09-20 7:20 Christian Pellegrin
2008-09-20 8:24 ` Andrew Morton
2008-09-20 10:35 ` chri
2008-09-20 13:56 ` Arjan van de Ven
2008-09-20 14:30 ` chri
2008-09-20 14:34 ` Alan Cox
2008-09-21 16:09 ` Ben Pfaff
2008-10-09 6:23 ` chri
2008-10-10 12:08 ` Christian Pellegrin
2008-09-20 14:11 ` Alan Cox
2008-09-20 14:37 ` chri
2008-10-09 6:30 ` chri
2008-10-09 9:18 ` Alan Cox
2008-09-20 10:51 Michael Trimarchi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).