Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Infinity Unlimited USB Phoenix driver |
| 3 | * |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk) |
| 5 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 6 | * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com) |
| 7 | * |
| 8 | * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * And tested with help of WB Electronics |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/tty.h> |
| 23 | #include <linux/tty_driver.h> |
| 24 | #include <linux/tty_flip.h> |
| 25 | #include <linux/serial.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | #include <linux/usb.h> |
| 31 | #include <linux/usb/serial.h> |
| 32 | #include "iuu_phoenix.h" |
| 33 | #include <linux/random.h> |
| 34 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Version Information |
| 37 | */ |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 38 | #define DRIVER_VERSION "v0.12" |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 39 | #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver" |
| 40 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 41 | static const struct usb_device_id id_table[] = { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 42 | {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)}, |
| 43 | {} /* Terminating entry */ |
| 44 | }; |
| 45 | MODULE_DEVICE_TABLE(usb, id_table); |
| 46 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 47 | /* turbo parameter */ |
| 48 | static int boost = 100; |
| 49 | static int clockmode = 1; |
| 50 | static int cdmode = 1; |
| 51 | static int iuu_cardin; |
| 52 | static int iuu_cardout; |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 53 | static bool xmas; |
Olivier Bornet | e55c6d0 | 2009-08-18 21:05:57 +0200 | [diff] [blame] | 54 | static int vcc_default = 5; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 55 | |
| 56 | static void read_rxcmd_callback(struct urb *urb); |
| 57 | |
| 58 | struct iuu_private { |
| 59 | spinlock_t lock; /* store irq state */ |
| 60 | wait_queue_head_t delta_msr_wait; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 61 | u8 line_status; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 62 | int tiostatus; /* store IUART SIGNAL for tiocmget call */ |
| 63 | u8 reset; /* if 1 reset is needed */ |
| 64 | int poll; /* number of poll */ |
| 65 | u8 *writebuf; /* buffer for writing to device */ |
| 66 | int writelen; /* num of byte to write to device */ |
| 67 | u8 *buf; /* used for initialize speed */ |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 68 | u8 len; |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 69 | int vcc; /* vcc (either 3 or 5 V) */ |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 70 | u32 baud; |
| 71 | u32 boost; |
| 72 | u32 clk; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | |
| 76 | static void iuu_free_buf(struct iuu_private *priv) |
| 77 | { |
| 78 | kfree(priv->buf); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 79 | kfree(priv->writebuf); |
| 80 | } |
| 81 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 82 | static int iuu_alloc_buf(struct usb_serial *serial, struct iuu_private *priv) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 83 | { |
| 84 | priv->buf = kzalloc(256, GFP_KERNEL); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 85 | priv->writebuf = kzalloc(256, GFP_KERNEL); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 86 | if (!priv->buf || !priv->writebuf) { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 87 | iuu_free_buf(priv); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 88 | dev_dbg(&serial->dev->dev, "%s problem allocation buffer\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 89 | return -ENOMEM; |
| 90 | } |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 91 | dev_dbg(&serial->dev->dev, "%s - Privates buffers allocation success\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int iuu_startup(struct usb_serial *serial) |
| 96 | { |
| 97 | struct iuu_private *priv; |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 98 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 99 | priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 100 | dev_dbg(&serial->dev->dev, "%s- priv allocation success\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 101 | if (!priv) |
| 102 | return -ENOMEM; |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 103 | if (iuu_alloc_buf(serial, priv)) { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 104 | kfree(priv); |
| 105 | return -ENOMEM; |
| 106 | } |
Olivier Bornet | e55c6d0 | 2009-08-18 21:05:57 +0200 | [diff] [blame] | 107 | priv->vcc = vcc_default; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 108 | spin_lock_init(&priv->lock); |
| 109 | init_waitqueue_head(&priv->delta_msr_wait); |
| 110 | usb_set_serial_port_data(serial->port[0], priv); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 114 | /* Release function */ |
| 115 | static void iuu_release(struct usb_serial *serial) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 116 | { |
| 117 | struct usb_serial_port *port = serial->port[0]; |
| 118 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 119 | if (!port) |
| 120 | return; |
| 121 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 122 | if (priv) { |
| 123 | iuu_free_buf(priv); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 124 | dev_dbg(&port->dev, "%s - I will free all\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 125 | usb_set_serial_port_data(port, NULL); |
| 126 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 127 | dev_dbg(&port->dev, "%s - priv is not anymore in port structure\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 128 | kfree(priv); |
| 129 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 130 | dev_dbg(&port->dev, "%s priv is now kfree\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 134 | static int iuu_tiocmset(struct tty_struct *tty, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 135 | unsigned int set, unsigned int clear) |
| 136 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 137 | struct usb_serial_port *port = tty->driver_data; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 138 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Alan Cox | 7b1fc8b | 2008-02-20 21:39:25 +0000 | [diff] [blame] | 139 | unsigned long flags; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 140 | |
Alan Cox | 7b1fc8b | 2008-02-20 21:39:25 +0000 | [diff] [blame] | 141 | /* FIXME: locking on tiomstatus */ |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 142 | dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n", |
| 143 | __func__, set, clear); |
Alan Cox | 7b1fc8b | 2008-02-20 21:39:25 +0000 | [diff] [blame] | 144 | |
| 145 | spin_lock_irqsave(&priv->lock, flags); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 146 | |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 147 | if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 148 | dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 149 | priv->reset = 1; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 150 | } |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 151 | if (set & TIOCM_RTS) |
| 152 | priv->tiostatus = TIOCM_RTS; |
| 153 | |
Alan Cox | 7b1fc8b | 2008-02-20 21:39:25 +0000 | [diff] [blame] | 154 | spin_unlock_irqrestore(&priv->lock, flags); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* This is used to provide a carrier detect mechanism |
| 159 | * When a card is present, the response is 0x00 |
| 160 | * When no card , the reader respond with TIOCM_CD |
| 161 | * This is known as CD autodetect mechanism |
| 162 | */ |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 163 | static int iuu_tiocmget(struct tty_struct *tty) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 164 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 165 | struct usb_serial_port *port = tty->driver_data; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 166 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Alan Cox | 7b1fc8b | 2008-02-20 21:39:25 +0000 | [diff] [blame] | 167 | unsigned long flags; |
| 168 | int rc; |
| 169 | |
| 170 | spin_lock_irqsave(&priv->lock, flags); |
| 171 | rc = priv->tiostatus; |
| 172 | spin_unlock_irqrestore(&priv->lock, flags); |
| 173 | |
| 174 | return rc; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void iuu_rxcmd(struct urb *urb) |
| 178 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 179 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 180 | int result; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 181 | int status = urb->status; |
| 182 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 183 | if (status) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 184 | dev_dbg(&port->dev, "%s - status = %d\n", __func__, status); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 185 | /* error stop all */ |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1); |
| 191 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 192 | usb_sndbulkpipe(port->serial->dev, |
| 193 | port->bulk_out_endpointAddress), |
| 194 | port->write_urb->transfer_buffer, 1, |
| 195 | read_rxcmd_callback, port); |
| 196 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 197 | } |
| 198 | |
| 199 | static int iuu_reset(struct usb_serial_port *port, u8 wt) |
| 200 | { |
| 201 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 202 | int result; |
| 203 | char *buf_ptr = port->write_urb->transfer_buffer; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 204 | |
| 205 | /* Prepare the reset sequence */ |
| 206 | |
| 207 | *buf_ptr++ = IUU_RST_SET; |
| 208 | *buf_ptr++ = IUU_DELAY_MS; |
| 209 | *buf_ptr++ = wt; |
| 210 | *buf_ptr = IUU_RST_CLEAR; |
| 211 | |
| 212 | /* send the sequence */ |
| 213 | |
| 214 | usb_fill_bulk_urb(port->write_urb, |
| 215 | port->serial->dev, |
| 216 | usb_sndbulkpipe(port->serial->dev, |
| 217 | port->bulk_out_endpointAddress), |
| 218 | port->write_urb->transfer_buffer, 4, iuu_rxcmd, port); |
| 219 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 220 | priv->reset = 0; |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | /* Status Function |
| 225 | * Return value is |
| 226 | * 0x00 = no card |
| 227 | * 0x01 = smartcard |
| 228 | * 0x02 = sim card |
| 229 | */ |
| 230 | static void iuu_update_status_callback(struct urb *urb) |
| 231 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 232 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 233 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 234 | u8 *st; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 235 | int status = urb->status; |
| 236 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 237 | if (status) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 238 | dev_dbg(&port->dev, "%s - status = %d\n", __func__, status); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 239 | /* error stop all */ |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | st = urb->transfer_buffer; |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 244 | dev_dbg(&port->dev, "%s - enter\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 245 | if (urb->actual_length == 1) { |
| 246 | switch (st[0]) { |
| 247 | case 0x1: |
| 248 | priv->tiostatus = iuu_cardout; |
| 249 | break; |
| 250 | case 0x0: |
| 251 | priv->tiostatus = iuu_cardin; |
| 252 | break; |
| 253 | default: |
| 254 | priv->tiostatus = iuu_cardin; |
| 255 | } |
| 256 | } |
| 257 | iuu_rxcmd(urb); |
| 258 | } |
| 259 | |
| 260 | static void iuu_status_callback(struct urb *urb) |
| 261 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 262 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 263 | int result; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 264 | int status = urb->status; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 265 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 266 | dev_dbg(&port->dev, "%s - status = %d\n", __func__, status); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 267 | usb_fill_bulk_urb(port->read_urb, port->serial->dev, |
| 268 | usb_rcvbulkpipe(port->serial->dev, |
| 269 | port->bulk_in_endpointAddress), |
| 270 | port->read_urb->transfer_buffer, 256, |
| 271 | iuu_update_status_callback, port); |
| 272 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 273 | } |
| 274 | |
| 275 | static int iuu_status(struct usb_serial_port *port) |
| 276 | { |
| 277 | int result; |
| 278 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 279 | memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1); |
| 280 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 281 | usb_sndbulkpipe(port->serial->dev, |
| 282 | port->bulk_out_endpointAddress), |
| 283 | port->write_urb->transfer_buffer, 1, |
| 284 | iuu_status_callback, port); |
| 285 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 286 | return result; |
| 287 | |
| 288 | } |
| 289 | |
| 290 | static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count) |
| 291 | { |
| 292 | int status; |
| 293 | struct usb_serial *serial = port->serial; |
| 294 | int actual = 0; |
| 295 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 296 | /* send the data out the bulk port */ |
| 297 | |
| 298 | status = |
| 299 | usb_bulk_msg(serial->dev, |
| 300 | usb_sndbulkpipe(serial->dev, |
| 301 | port->bulk_out_endpointAddress), buf, |
| 302 | count, &actual, HZ * 1); |
| 303 | |
Alan Cox | 9e8e2d2 | 2008-07-22 11:12:59 +0100 | [diff] [blame] | 304 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 305 | dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status); |
Alan Cox | 9e8e2d2 | 2008-07-22 11:12:59 +0100 | [diff] [blame] | 306 | else |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 307 | dev_dbg(&port->dev, "%s - write OK !\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 308 | return status; |
| 309 | } |
| 310 | |
| 311 | static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count) |
| 312 | { |
| 313 | int status; |
| 314 | struct usb_serial *serial = port->serial; |
| 315 | int actual = 0; |
| 316 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 317 | /* send the data out the bulk port */ |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 318 | status = |
| 319 | usb_bulk_msg(serial->dev, |
| 320 | usb_rcvbulkpipe(serial->dev, |
| 321 | port->bulk_in_endpointAddress), buf, |
| 322 | count, &actual, HZ * 1); |
| 323 | |
Alan Cox | 9e8e2d2 | 2008-07-22 11:12:59 +0100 | [diff] [blame] | 324 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 325 | dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status); |
Alan Cox | 9e8e2d2 | 2008-07-22 11:12:59 +0100 | [diff] [blame] | 326 | else |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 327 | dev_dbg(&port->dev, "%s - read OK !\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 328 | return status; |
| 329 | } |
| 330 | |
| 331 | static int iuu_led(struct usb_serial_port *port, unsigned int R, |
| 332 | unsigned int G, unsigned int B, u8 f) |
| 333 | { |
| 334 | int status; |
| 335 | u8 *buf; |
| 336 | buf = kmalloc(8, GFP_KERNEL); |
| 337 | if (!buf) |
| 338 | return -ENOMEM; |
| 339 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 340 | buf[0] = IUU_SET_LED; |
| 341 | buf[1] = R & 0xFF; |
| 342 | buf[2] = (R >> 8) & 0xFF; |
| 343 | buf[3] = G & 0xFF; |
| 344 | buf[4] = (G >> 8) & 0xFF; |
| 345 | buf[5] = B & 0xFF; |
| 346 | buf[6] = (B >> 8) & 0xFF; |
| 347 | buf[7] = f; |
| 348 | status = bulk_immediate(port, buf, 8); |
| 349 | kfree(buf); |
| 350 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 351 | dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 352 | else |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 353 | dev_dbg(&port->dev, "%s - led OK !\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 354 | return IUU_OPERATION_OK; |
| 355 | } |
| 356 | |
| 357 | static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1, |
| 358 | u8 b2, u8 freq) |
| 359 | { |
| 360 | *buf++ = IUU_SET_LED; |
| 361 | *buf++ = r1; |
| 362 | *buf++ = r2; |
| 363 | *buf++ = g1; |
| 364 | *buf++ = g2; |
| 365 | *buf++ = b1; |
| 366 | *buf++ = b2; |
| 367 | *buf = freq; |
| 368 | } |
| 369 | |
| 370 | static void iuu_led_activity_on(struct urb *urb) |
| 371 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 372 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 373 | int result; |
| 374 | char *buf_ptr = port->write_urb->transfer_buffer; |
| 375 | *buf_ptr++ = IUU_SET_LED; |
| 376 | if (xmas == 1) { |
| 377 | get_random_bytes(buf_ptr, 6); |
| 378 | *(buf_ptr+7) = 1; |
| 379 | } else { |
| 380 | iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255); |
| 381 | } |
| 382 | |
| 383 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 384 | usb_sndbulkpipe(port->serial->dev, |
| 385 | port->bulk_out_endpointAddress), |
| 386 | port->write_urb->transfer_buffer, 8 , |
| 387 | iuu_rxcmd, port); |
| 388 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 389 | } |
| 390 | |
| 391 | static void iuu_led_activity_off(struct urb *urb) |
| 392 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 393 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 394 | int result; |
| 395 | char *buf_ptr = port->write_urb->transfer_buffer; |
| 396 | if (xmas == 1) { |
| 397 | iuu_rxcmd(urb); |
| 398 | return; |
| 399 | } else { |
| 400 | *buf_ptr++ = IUU_SET_LED; |
| 401 | iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255); |
| 402 | } |
| 403 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 404 | usb_sndbulkpipe(port->serial->dev, |
| 405 | port->bulk_out_endpointAddress), |
| 406 | port->write_urb->transfer_buffer, 8 , |
| 407 | iuu_rxcmd, port); |
| 408 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | static int iuu_clk(struct usb_serial_port *port, int dwFrq) |
| 414 | { |
| 415 | int status; |
| 416 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 417 | int Count = 0; |
| 418 | u8 FrqGenAdr = 0x69; |
| 419 | u8 DIV = 0; /* 8bit */ |
| 420 | u8 XDRV = 0; /* 8bit */ |
| 421 | u8 PUMP = 0; /* 3bit */ |
| 422 | u8 PBmsb = 0; /* 2bit */ |
| 423 | u8 PBlsb = 0; /* 8bit */ |
| 424 | u8 PO = 0; /* 1bit */ |
| 425 | u8 Q = 0; /* 7bit */ |
| 426 | /* 24bit = 3bytes */ |
| 427 | unsigned int P = 0; |
| 428 | unsigned int P2 = 0; |
| 429 | int frq = (int)dwFrq; |
| 430 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 431 | if (frq == 0) { |
| 432 | priv->buf[Count++] = IUU_UART_WRITE_I2C; |
| 433 | priv->buf[Count++] = FrqGenAdr << 1; |
| 434 | priv->buf[Count++] = 0x09; |
| 435 | priv->buf[Count++] = 0x00; |
| 436 | |
| 437 | status = bulk_immediate(port, (u8 *) priv->buf, Count); |
| 438 | if (status != 0) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 439 | dev_dbg(&port->dev, "%s - write error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 440 | return status; |
| 441 | } |
| 442 | } else if (frq == 3579000) { |
| 443 | DIV = 100; |
| 444 | P = 1193; |
| 445 | Q = 40; |
| 446 | XDRV = 0; |
| 447 | } else if (frq == 3680000) { |
| 448 | DIV = 105; |
| 449 | P = 161; |
| 450 | Q = 5; |
| 451 | XDRV = 0; |
| 452 | } else if (frq == 6000000) { |
| 453 | DIV = 66; |
| 454 | P = 66; |
| 455 | Q = 2; |
| 456 | XDRV = 0x28; |
| 457 | } else { |
| 458 | unsigned int result = 0; |
| 459 | unsigned int tmp = 0; |
| 460 | unsigned int check; |
| 461 | unsigned int check2; |
| 462 | char found = 0x00; |
| 463 | unsigned int lQ = 2; |
| 464 | unsigned int lP = 2055; |
| 465 | unsigned int lDiv = 4; |
| 466 | |
| 467 | for (lQ = 2; lQ <= 47 && !found; lQ++) |
| 468 | for (lP = 2055; lP >= 8 && !found; lP--) |
| 469 | for (lDiv = 4; lDiv <= 127 && !found; lDiv++) { |
| 470 | tmp = (12000000 / lDiv) * (lP / lQ); |
| 471 | if (abs((int)(tmp - frq)) < |
| 472 | abs((int)(frq - result))) { |
| 473 | check2 = (12000000 / lQ); |
| 474 | if (check2 < 250000) |
| 475 | continue; |
| 476 | check = (12000000 / lQ) * lP; |
| 477 | if (check > 400000000) |
| 478 | continue; |
| 479 | if (check < 100000000) |
| 480 | continue; |
| 481 | if (lDiv < 4 || lDiv > 127) |
| 482 | continue; |
| 483 | result = tmp; |
| 484 | P = lP; |
| 485 | DIV = lDiv; |
| 486 | Q = lQ; |
| 487 | if (result == frq) |
| 488 | found = 0x01; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | P2 = ((P - PO) / 2) - 4; |
| 493 | DIV = DIV; |
| 494 | PUMP = 0x04; |
| 495 | PBmsb = (P2 >> 8 & 0x03); |
| 496 | PBlsb = P2 & 0xFF; |
| 497 | PO = (P >> 10) & 0x01; |
| 498 | Q = Q - 2; |
| 499 | |
| 500 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 501 | priv->buf[Count++] = FrqGenAdr << 1; |
| 502 | priv->buf[Count++] = 0x09; |
| 503 | priv->buf[Count++] = 0x20; /* Adr = 0x09 */ |
| 504 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 505 | priv->buf[Count++] = FrqGenAdr << 1; |
| 506 | priv->buf[Count++] = 0x0C; |
| 507 | priv->buf[Count++] = DIV; /* Adr = 0x0C */ |
| 508 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 509 | priv->buf[Count++] = FrqGenAdr << 1; |
| 510 | priv->buf[Count++] = 0x12; |
| 511 | priv->buf[Count++] = XDRV; /* Adr = 0x12 */ |
| 512 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 513 | priv->buf[Count++] = FrqGenAdr << 1; |
| 514 | priv->buf[Count++] = 0x13; |
| 515 | priv->buf[Count++] = 0x6B; /* Adr = 0x13 */ |
| 516 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 517 | priv->buf[Count++] = FrqGenAdr << 1; |
| 518 | priv->buf[Count++] = 0x40; |
| 519 | priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) | |
| 520 | (PBmsb & 0x03); /* Adr = 0x40 */ |
| 521 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 522 | priv->buf[Count++] = FrqGenAdr << 1; |
| 523 | priv->buf[Count++] = 0x41; |
| 524 | priv->buf[Count++] = PBlsb; /* Adr = 0x41 */ |
| 525 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 526 | priv->buf[Count++] = FrqGenAdr << 1; |
| 527 | priv->buf[Count++] = 0x42; |
| 528 | priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */ |
| 529 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 530 | priv->buf[Count++] = FrqGenAdr << 1; |
| 531 | priv->buf[Count++] = 0x44; |
| 532 | priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */ |
| 533 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 534 | priv->buf[Count++] = FrqGenAdr << 1; |
| 535 | priv->buf[Count++] = 0x45; |
| 536 | priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */ |
| 537 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 538 | priv->buf[Count++] = FrqGenAdr << 1; |
| 539 | priv->buf[Count++] = 0x46; |
| 540 | priv->buf[Count++] = 0x7F; /* Adr = 0x46 */ |
| 541 | priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */ |
| 542 | priv->buf[Count++] = FrqGenAdr << 1; |
| 543 | priv->buf[Count++] = 0x47; |
| 544 | priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */ |
| 545 | |
| 546 | status = bulk_immediate(port, (u8 *) priv->buf, Count); |
| 547 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 548 | dev_dbg(&port->dev, "%s - write error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 549 | return status; |
| 550 | } |
| 551 | |
| 552 | static int iuu_uart_flush(struct usb_serial_port *port) |
| 553 | { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 554 | struct device *dev = &port->dev; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 555 | int i; |
| 556 | int status; |
| 557 | u8 rxcmd = IUU_UART_RX; |
| 558 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 559 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 560 | if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0) |
| 561 | return -EIO; |
| 562 | |
| 563 | for (i = 0; i < 2; i++) { |
| 564 | status = bulk_immediate(port, &rxcmd, 1); |
| 565 | if (status != IUU_OPERATION_OK) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 566 | dev_dbg(dev, "%s - uart_flush_write error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 567 | return status; |
| 568 | } |
| 569 | |
| 570 | status = read_immediate(port, &priv->len, 1); |
| 571 | if (status != IUU_OPERATION_OK) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 572 | dev_dbg(dev, "%s - uart_flush_read error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 573 | return status; |
| 574 | } |
| 575 | |
| 576 | if (priv->len > 0) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 577 | dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 578 | status = read_immediate(port, priv->buf, priv->len); |
| 579 | if (status != IUU_OPERATION_OK) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 580 | dev_dbg(dev, "%s - uart_flush_read error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 581 | return status; |
| 582 | } |
| 583 | } |
| 584 | } |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 585 | dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 586 | iuu_led(port, 0, 0xF000, 0, 0xFF); |
| 587 | return status; |
| 588 | } |
| 589 | |
| 590 | static void read_buf_callback(struct urb *urb) |
| 591 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 592 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 593 | unsigned char *data = urb->transfer_buffer; |
| 594 | struct tty_struct *tty; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 595 | int status = urb->status; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 596 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 597 | if (status) { |
| 598 | if (status == -EPROTO) { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 599 | /* reschedule needed */ |
| 600 | } |
| 601 | return; |
| 602 | } |
| 603 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 604 | dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 605 | tty = tty_port_tty_get(&port->port); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 606 | if (data == NULL) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 607 | dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 608 | if (tty && urb->actual_length && data) { |
| 609 | tty_insert_flip_string(tty, data, urb->actual_length); |
| 610 | tty_flip_buffer_push(tty); |
| 611 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 612 | tty_kref_put(tty); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 613 | iuu_led_activity_on(urb); |
| 614 | } |
| 615 | |
| 616 | static int iuu_bulk_write(struct usb_serial_port *port) |
| 617 | { |
| 618 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Steven Rostedt | 8594303 | 2008-05-06 20:42:31 -0700 | [diff] [blame] | 619 | unsigned long flags; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 620 | int result; |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 621 | int buf_len; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 622 | char *buf_ptr = port->write_urb->transfer_buffer; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 623 | |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 624 | spin_lock_irqsave(&priv->lock, flags); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 625 | *buf_ptr++ = IUU_UART_ESC; |
| 626 | *buf_ptr++ = IUU_UART_TX; |
| 627 | *buf_ptr++ = priv->writelen; |
| 628 | |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 629 | memcpy(buf_ptr, priv->writebuf, priv->writelen); |
| 630 | buf_len = priv->writelen; |
| 631 | priv->writelen = 0; |
| 632 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 633 | dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__, |
| 634 | buf_len, buf_len, buf_ptr); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 635 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 636 | usb_sndbulkpipe(port->serial->dev, |
| 637 | port->bulk_out_endpointAddress), |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 638 | port->write_urb->transfer_buffer, buf_len + 3, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 639 | iuu_rxcmd, port); |
| 640 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 641 | usb_serial_port_softint(port); |
| 642 | return result; |
| 643 | } |
| 644 | |
| 645 | static int iuu_read_buf(struct usb_serial_port *port, int len) |
| 646 | { |
| 647 | int result; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 648 | |
| 649 | usb_fill_bulk_urb(port->read_urb, port->serial->dev, |
| 650 | usb_rcvbulkpipe(port->serial->dev, |
| 651 | port->bulk_in_endpointAddress), |
| 652 | port->read_urb->transfer_buffer, len, |
| 653 | read_buf_callback, port); |
| 654 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 655 | return result; |
| 656 | } |
| 657 | |
| 658 | static void iuu_uart_read_callback(struct urb *urb) |
| 659 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 660 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 661 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Steven Rostedt | 8594303 | 2008-05-06 20:42:31 -0700 | [diff] [blame] | 662 | unsigned long flags; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 663 | int status = urb->status; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 664 | int error = 0; |
| 665 | int len = 0; |
| 666 | unsigned char *data = urb->transfer_buffer; |
| 667 | priv->poll++; |
| 668 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 669 | if (status) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 670 | dev_dbg(&port->dev, "%s - status = %d\n", __func__, status); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 671 | /* error stop all */ |
| 672 | return; |
| 673 | } |
| 674 | if (data == NULL) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 675 | dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 676 | |
| 677 | if (urb->actual_length == 1 && data != NULL) |
| 678 | len = (int) data[0]; |
| 679 | |
| 680 | if (urb->actual_length > 1) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 681 | dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 682 | urb->actual_length); |
| 683 | error = 1; |
| 684 | return; |
| 685 | } |
| 686 | /* if len > 0 call readbuf */ |
| 687 | |
| 688 | if (len > 0 && error == 0) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 689 | dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 690 | __func__, len); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 691 | status = iuu_read_buf(port, len); |
| 692 | return; |
| 693 | } |
| 694 | /* need to update status ? */ |
| 695 | if (priv->poll > 99) { |
| 696 | status = iuu_status(port); |
| 697 | priv->poll = 0; |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | /* reset waiting ? */ |
| 702 | |
| 703 | if (priv->reset == 1) { |
| 704 | status = iuu_reset(port, 0xC); |
| 705 | return; |
| 706 | } |
| 707 | /* Writebuf is waiting */ |
| 708 | spin_lock_irqsave(&priv->lock, flags); |
| 709 | if (priv->writelen > 0) { |
| 710 | spin_unlock_irqrestore(&priv->lock, flags); |
| 711 | status = iuu_bulk_write(port); |
| 712 | return; |
| 713 | } |
| 714 | spin_unlock_irqrestore(&priv->lock, flags); |
| 715 | /* if nothing to write call again rxcmd */ |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 716 | dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 717 | iuu_led_activity_off(urb); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 720 | static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 721 | const u8 *buf, int count) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 722 | { |
| 723 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Steven Rostedt | 8594303 | 2008-05-06 20:42:31 -0700 | [diff] [blame] | 724 | unsigned long flags; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 725 | |
| 726 | if (count > 256) |
| 727 | return -ENOMEM; |
| 728 | |
| 729 | spin_lock_irqsave(&priv->lock, flags); |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 730 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 731 | /* fill the buffer */ |
Olivier Bornet | 5fcf62b | 2009-06-11 12:52:26 +0100 | [diff] [blame] | 732 | memcpy(priv->writebuf + priv->writelen, buf, count); |
| 733 | priv->writelen += count; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 734 | spin_unlock_irqrestore(&priv->lock, flags); |
| 735 | |
Alan Cox | 9e8e2d2 | 2008-07-22 11:12:59 +0100 | [diff] [blame] | 736 | return count; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static void read_rxcmd_callback(struct urb *urb) |
| 740 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 741 | struct usb_serial_port *port = urb->context; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 742 | int result; |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 743 | int status = urb->status; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 744 | |
Greg Kroah-Hartman | 50de36f | 2008-12-10 16:00:30 -0800 | [diff] [blame] | 745 | if (status) { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 746 | /* error stop all */ |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | usb_fill_bulk_urb(port->read_urb, port->serial->dev, |
| 751 | usb_rcvbulkpipe(port->serial->dev, |
| 752 | port->bulk_in_endpointAddress), |
| 753 | port->read_urb->transfer_buffer, 256, |
| 754 | iuu_uart_read_callback, port); |
| 755 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 756 | dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int iuu_uart_on(struct usb_serial_port *port) |
| 760 | { |
| 761 | int status; |
| 762 | u8 *buf; |
| 763 | |
| 764 | buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL); |
| 765 | |
| 766 | if (!buf) |
| 767 | return -ENOMEM; |
| 768 | |
| 769 | buf[0] = IUU_UART_ENABLE; |
| 770 | buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF); |
| 771 | buf[2] = (u8) (0x00FF & IUU_BAUD_9600); |
Olivier Bornet | cc3447d | 2009-06-11 12:53:24 +0100 | [diff] [blame] | 772 | buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 773 | |
| 774 | status = bulk_immediate(port, buf, 4); |
| 775 | if (status != IUU_OPERATION_OK) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 776 | dev_dbg(&port->dev, "%s - uart_on error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 777 | goto uart_enable_failed; |
| 778 | } |
| 779 | /* iuu_reset() the card after iuu_uart_on() */ |
| 780 | status = iuu_uart_flush(port); |
| 781 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 782 | dev_dbg(&port->dev, "%s - uart_flush error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 783 | uart_enable_failed: |
| 784 | kfree(buf); |
| 785 | return status; |
| 786 | } |
| 787 | |
| 788 | /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */ |
| 789 | static int iuu_uart_off(struct usb_serial_port *port) |
| 790 | { |
| 791 | int status; |
| 792 | u8 *buf; |
| 793 | buf = kmalloc(1, GFP_KERNEL); |
| 794 | if (!buf) |
| 795 | return -ENOMEM; |
| 796 | buf[0] = IUU_UART_DISABLE; |
| 797 | |
| 798 | status = bulk_immediate(port, buf, 1); |
| 799 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 800 | dev_dbg(&port->dev, "%s - uart_off error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 801 | |
| 802 | kfree(buf); |
| 803 | return status; |
| 804 | } |
| 805 | |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 806 | static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 807 | u32 *actual, u8 parity) |
| 808 | { |
| 809 | int status; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 810 | u32 baud; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 811 | u8 *dataout; |
| 812 | u8 DataCount = 0; |
| 813 | u8 T1Frekvens = 0; |
| 814 | u8 T1reload = 0; |
| 815 | unsigned int T1FrekvensHZ = 0; |
| 816 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 817 | dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 818 | dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL); |
| 819 | |
| 820 | if (!dataout) |
| 821 | return -ENOMEM; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 822 | /*baud = (((priv->clk / 35) * baud_base) / 100000); */ |
| 823 | baud = baud_base; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 824 | |
| 825 | if (baud < 1200 || baud > 230400) { |
| 826 | kfree(dataout); |
| 827 | return IUU_INVALID_PARAMETER; |
| 828 | } |
| 829 | if (baud > 977) { |
| 830 | T1Frekvens = 3; |
| 831 | T1FrekvensHZ = 500000; |
| 832 | } |
| 833 | |
| 834 | if (baud > 3906) { |
| 835 | T1Frekvens = 2; |
| 836 | T1FrekvensHZ = 2000000; |
| 837 | } |
| 838 | |
| 839 | if (baud > 11718) { |
| 840 | T1Frekvens = 1; |
| 841 | T1FrekvensHZ = 6000000; |
| 842 | } |
| 843 | |
| 844 | if (baud > 46875) { |
| 845 | T1Frekvens = 0; |
| 846 | T1FrekvensHZ = 24000000; |
| 847 | } |
| 848 | |
| 849 | T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2)); |
| 850 | |
| 851 | /* magic number here: ENTER_FIRMWARE_UPDATE; */ |
| 852 | dataout[DataCount++] = IUU_UART_ESC; |
| 853 | /* magic number here: CHANGE_BAUD; */ |
| 854 | dataout[DataCount++] = IUU_UART_CHANGE; |
| 855 | dataout[DataCount++] = T1Frekvens; |
| 856 | dataout[DataCount++] = T1reload; |
| 857 | |
| 858 | *actual = (T1FrekvensHZ / (256 - T1reload)) / 2; |
| 859 | |
| 860 | switch (parity & 0x0F) { |
| 861 | case IUU_PARITY_NONE: |
| 862 | dataout[DataCount++] = 0x00; |
| 863 | break; |
| 864 | case IUU_PARITY_EVEN: |
| 865 | dataout[DataCount++] = 0x01; |
| 866 | break; |
| 867 | case IUU_PARITY_ODD: |
| 868 | dataout[DataCount++] = 0x02; |
| 869 | break; |
| 870 | case IUU_PARITY_MARK: |
| 871 | dataout[DataCount++] = 0x03; |
| 872 | break; |
| 873 | case IUU_PARITY_SPACE: |
| 874 | dataout[DataCount++] = 0x04; |
| 875 | break; |
| 876 | default: |
| 877 | kfree(dataout); |
| 878 | return IUU_INVALID_PARAMETER; |
| 879 | break; |
| 880 | } |
| 881 | |
| 882 | switch (parity & 0xF0) { |
| 883 | case IUU_ONE_STOP_BIT: |
| 884 | dataout[DataCount - 1] |= IUU_ONE_STOP_BIT; |
| 885 | break; |
| 886 | |
| 887 | case IUU_TWO_STOP_BITS: |
| 888 | dataout[DataCount - 1] |= IUU_TWO_STOP_BITS; |
| 889 | break; |
| 890 | default: |
| 891 | kfree(dataout); |
| 892 | return IUU_INVALID_PARAMETER; |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | status = bulk_immediate(port, dataout, DataCount); |
| 897 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 898 | dev_dbg(&port->dev, "%s - uart_off error\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 899 | kfree(dataout); |
| 900 | return status; |
| 901 | } |
| 902 | |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 903 | static void iuu_set_termios(struct tty_struct *tty, |
| 904 | struct usb_serial_port *port, struct ktermios *old_termios) |
| 905 | { |
| 906 | const u32 supported_mask = CMSPAR|PARENB|PARODD; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 907 | struct iuu_private *priv = usb_get_serial_port_data(port); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 908 | unsigned int cflag = tty->termios.c_cflag; |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 909 | int status; |
| 910 | u32 actual; |
| 911 | u32 parity; |
| 912 | int csize = CS7; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 913 | int baud; |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 914 | u32 newval = cflag & supported_mask; |
| 915 | |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 916 | /* Just use the ospeed. ispeed should be the same. */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 917 | baud = tty->termios.c_ospeed; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 918 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 919 | dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud); |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 920 | |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 921 | /* compute the parity parameter */ |
| 922 | parity = 0; |
| 923 | if (cflag & CMSPAR) { /* Using mark space */ |
| 924 | if (cflag & PARODD) |
| 925 | parity |= IUU_PARITY_SPACE; |
| 926 | else |
| 927 | parity |= IUU_PARITY_MARK; |
| 928 | } else if (!(cflag & PARENB)) { |
| 929 | parity |= IUU_PARITY_NONE; |
| 930 | csize = CS8; |
| 931 | } else if (cflag & PARODD) |
| 932 | parity |= IUU_PARITY_ODD; |
| 933 | else |
| 934 | parity |= IUU_PARITY_EVEN; |
| 935 | |
| 936 | parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT); |
| 937 | |
| 938 | /* set it */ |
| 939 | status = iuu_uart_baud(port, |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 940 | baud * priv->boost / 100, |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 941 | &actual, parity); |
| 942 | |
| 943 | /* set the termios value to the real one, so the user now what has |
| 944 | * changed. We support few fields so its easies to copy the old hw |
| 945 | * settings back over and then adjust them |
| 946 | */ |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 947 | if (old_termios) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 948 | tty_termios_copy_hw(&tty->termios, old_termios); |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 949 | if (status != 0) /* Set failed - return old bits */ |
| 950 | return; |
| 951 | /* Re-encode speed, parity and csize */ |
| 952 | tty_encode_baud_rate(tty, baud, baud); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 953 | tty->termios.c_cflag &= ~(supported_mask|CSIZE); |
| 954 | tty->termios.c_cflag |= newval | csize; |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 957 | static void iuu_close(struct usb_serial_port *port) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 958 | { |
| 959 | /* iuu_led (port,255,0,0,0); */ |
| 960 | struct usb_serial *serial; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 961 | |
| 962 | serial = port->serial; |
| 963 | if (!serial) |
| 964 | return; |
| 965 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 966 | iuu_uart_off(port); |
| 967 | if (serial->dev) { |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 968 | /* free writebuf */ |
| 969 | /* shutdown our urbs */ |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 970 | dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 971 | usb_kill_urb(port->write_urb); |
| 972 | usb_kill_urb(port->read_urb); |
| 973 | usb_kill_urb(port->interrupt_in_urb); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 974 | iuu_led(port, 0, 0, 0xF000, 0xFF); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 978 | static void iuu_init_termios(struct tty_struct *tty) |
| 979 | { |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 980 | tty->termios = tty_std_termios; |
| 981 | tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600 |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 982 | | TIOCM_CTS | CSTOPB | PARENB; |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 983 | tty->termios.c_ispeed = 9600; |
| 984 | tty->termios.c_ospeed = 9600; |
| 985 | tty->termios.c_lflag = 0; |
| 986 | tty->termios.c_oflag = 0; |
| 987 | tty->termios.c_iflag = 0; |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 990 | static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 991 | { |
| 992 | struct usb_serial *serial = port->serial; |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 993 | struct device *dev = &port->dev; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 994 | u8 *buf; |
| 995 | int result; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 996 | int baud; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 997 | u32 actual; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 998 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 999 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1000 | baud = tty->termios.c_ospeed; |
| 1001 | tty->termios.c_ispeed = baud; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1002 | /* Re-encode speed */ |
| 1003 | tty_encode_baud_rate(tty, baud, baud); |
| 1004 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1005 | dev_dbg(dev, "%s - baud %d\n", __func__, baud); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1006 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 1007 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 1008 | |
| 1009 | buf = kmalloc(10, GFP_KERNEL); |
| 1010 | if (buf == NULL) |
| 1011 | return -ENOMEM; |
| 1012 | |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1013 | priv->poll = 0; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1014 | |
| 1015 | /* initialize writebuf */ |
| 1016 | #define FISH(a, b, c, d) do { \ |
| 1017 | result = usb_control_msg(port->serial->dev, \ |
| 1018 | usb_rcvctrlpipe(port->serial->dev, 0), \ |
| 1019 | b, a, c, d, buf, 1, 1000); \ |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1020 | dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \ |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1021 | buf[0]); } while (0); |
| 1022 | |
| 1023 | #define SOUP(a, b, c, d) do { \ |
| 1024 | result = usb_control_msg(port->serial->dev, \ |
| 1025 | usb_sndctrlpipe(port->serial->dev, 0), \ |
| 1026 | b, a, c, d, NULL, 0, 1000); \ |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1027 | dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0) |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1028 | |
| 1029 | /* This is not UART related but IUU USB driver related or something */ |
| 1030 | /* like that. Basically no IUU will accept any commands from the USB */ |
| 1031 | /* host unless it has received the following message */ |
| 1032 | /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */ |
| 1033 | |
| 1034 | SOUP(0x03, 0x02, 0x02, 0x0); |
| 1035 | kfree(buf); |
| 1036 | iuu_led(port, 0xF000, 0xF000, 0, 0xFF); |
| 1037 | iuu_uart_on(port); |
| 1038 | if (boost < 100) |
| 1039 | boost = 100; |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1040 | priv->boost = boost; |
| 1041 | priv->baud = baud; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1042 | switch (clockmode) { |
| 1043 | case 2: /* 3.680 Mhz */ |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1044 | priv->clk = IUU_CLK_3680000; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1045 | iuu_clk(port, IUU_CLK_3680000 * boost / 100); |
| 1046 | result = |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1047 | iuu_uart_baud(port, baud * boost / 100, &actual, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1048 | IUU_PARITY_EVEN); |
| 1049 | break; |
| 1050 | case 3: /* 6.00 Mhz */ |
| 1051 | iuu_clk(port, IUU_CLK_6000000 * boost / 100); |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1052 | priv->clk = IUU_CLK_6000000; |
| 1053 | /* Ratio of 6000000 to 3500000 for baud 9600 */ |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1054 | result = |
| 1055 | iuu_uart_baud(port, 16457 * boost / 100, &actual, |
| 1056 | IUU_PARITY_EVEN); |
| 1057 | break; |
| 1058 | default: /* 3.579 Mhz */ |
| 1059 | iuu_clk(port, IUU_CLK_3579000 * boost / 100); |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1060 | priv->clk = IUU_CLK_3579000; |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1061 | result = |
James Courtier-Dutton | 89b5439 | 2010-05-21 11:53:25 +0100 | [diff] [blame] | 1062 | iuu_uart_baud(port, baud * boost / 100, &actual, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1063 | IUU_PARITY_EVEN); |
| 1064 | } |
| 1065 | |
| 1066 | /* set the cardin cardout signals */ |
| 1067 | switch (cdmode) { |
| 1068 | case 0: |
| 1069 | iuu_cardin = 0; |
| 1070 | iuu_cardout = 0; |
| 1071 | break; |
| 1072 | case 1: |
| 1073 | iuu_cardin = TIOCM_CD; |
| 1074 | iuu_cardout = 0; |
| 1075 | break; |
| 1076 | case 2: |
| 1077 | iuu_cardin = 0; |
| 1078 | iuu_cardout = TIOCM_CD; |
| 1079 | break; |
| 1080 | case 3: |
| 1081 | iuu_cardin = TIOCM_DSR; |
| 1082 | iuu_cardout = 0; |
| 1083 | break; |
| 1084 | case 4: |
| 1085 | iuu_cardin = 0; |
| 1086 | iuu_cardout = TIOCM_DSR; |
| 1087 | break; |
| 1088 | case 5: |
| 1089 | iuu_cardin = TIOCM_CTS; |
| 1090 | iuu_cardout = 0; |
| 1091 | break; |
| 1092 | case 6: |
| 1093 | iuu_cardin = 0; |
| 1094 | iuu_cardout = TIOCM_CTS; |
| 1095 | break; |
| 1096 | case 7: |
| 1097 | iuu_cardin = TIOCM_RNG; |
| 1098 | iuu_cardout = 0; |
| 1099 | break; |
| 1100 | case 8: |
| 1101 | iuu_cardin = 0; |
| 1102 | iuu_cardout = TIOCM_RNG; |
| 1103 | } |
| 1104 | |
| 1105 | iuu_uart_flush(port); |
| 1106 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1107 | dev_dbg(dev, "%s - initialization done\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1108 | |
| 1109 | memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1); |
| 1110 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 1111 | usb_sndbulkpipe(port->serial->dev, |
| 1112 | port->bulk_out_endpointAddress), |
| 1113 | port->write_urb->transfer_buffer, 1, |
| 1114 | read_rxcmd_callback, port); |
| 1115 | result = usb_submit_urb(port->write_urb, GFP_KERNEL); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1116 | if (result) { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1117 | dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result); |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 1118 | iuu_close(port); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1119 | } else { |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1120 | dev_dbg(dev, "%s - rxcmd OK\n", __func__); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1121 | } |
Johan Hovold | b58a646 | 2011-11-10 14:58:30 +0100 | [diff] [blame] | 1122 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1123 | return result; |
| 1124 | } |
| 1125 | |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1126 | /* how to change VCC */ |
| 1127 | static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc) |
| 1128 | { |
| 1129 | int status; |
| 1130 | u8 *buf; |
| 1131 | |
| 1132 | buf = kmalloc(5, GFP_KERNEL); |
| 1133 | if (!buf) |
| 1134 | return -ENOMEM; |
| 1135 | |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1136 | buf[0] = IUU_SET_VCC; |
| 1137 | buf[1] = vcc & 0xFF; |
| 1138 | buf[2] = (vcc >> 8) & 0xFF; |
| 1139 | buf[3] = (vcc >> 16) & 0xFF; |
| 1140 | buf[4] = (vcc >> 24) & 0xFF; |
| 1141 | |
| 1142 | status = bulk_immediate(port, buf, 5); |
| 1143 | kfree(buf); |
| 1144 | |
| 1145 | if (status != IUU_OPERATION_OK) |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1146 | dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status); |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1147 | else |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1148 | dev_dbg(&port->dev, "%s - vcc OK !\n", __func__); |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1149 | |
| 1150 | return status; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Sysfs Attributes |
| 1155 | */ |
| 1156 | |
| 1157 | static ssize_t show_vcc_mode(struct device *dev, |
| 1158 | struct device_attribute *attr, char *buf) |
| 1159 | { |
| 1160 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 1161 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 1162 | |
| 1163 | return sprintf(buf, "%d\n", priv->vcc); |
| 1164 | } |
| 1165 | |
| 1166 | static ssize_t store_vcc_mode(struct device *dev, |
| 1167 | struct device_attribute *attr, const char *buf, size_t count) |
| 1168 | { |
| 1169 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 1170 | struct iuu_private *priv = usb_get_serial_port_data(port); |
| 1171 | unsigned long v; |
| 1172 | |
| 1173 | if (strict_strtoul(buf, 10, &v)) { |
| 1174 | dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n", |
| 1175 | __func__, buf); |
| 1176 | goto fail_store_vcc_mode; |
| 1177 | } |
| 1178 | |
Greg Kroah-Hartman | 2621cee | 2012-09-14 15:08:28 -0700 | [diff] [blame] | 1179 | dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v); |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1180 | |
| 1181 | if ((v != 3) && (v != 5)) { |
| 1182 | dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v); |
| 1183 | } else { |
| 1184 | iuu_vcc_set(port, v); |
| 1185 | priv->vcc = v; |
| 1186 | } |
| 1187 | fail_store_vcc_mode: |
| 1188 | return count; |
| 1189 | } |
| 1190 | |
| 1191 | static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode, |
| 1192 | store_vcc_mode); |
| 1193 | |
| 1194 | static int iuu_create_sysfs_attrs(struct usb_serial_port *port) |
| 1195 | { |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1196 | return device_create_file(&port->dev, &dev_attr_vcc_mode); |
| 1197 | } |
| 1198 | |
| 1199 | static int iuu_remove_sysfs_attrs(struct usb_serial_port *port) |
| 1200 | { |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1201 | device_remove_file(&port->dev, &dev_attr_vcc_mode); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * End Sysfs Attributes |
| 1207 | */ |
| 1208 | |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1209 | static struct usb_serial_driver iuu_device = { |
| 1210 | .driver = { |
| 1211 | .owner = THIS_MODULE, |
| 1212 | .name = "iuu_phoenix", |
| 1213 | }, |
| 1214 | .id_table = id_table, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1215 | .num_ports = 1, |
Johan Hovold | bbcb2b9 | 2010-03-17 23:00:37 +0100 | [diff] [blame] | 1216 | .bulk_in_size = 512, |
| 1217 | .bulk_out_size = 512, |
Olivier Bornet | 20eda94 | 2009-08-18 21:05:55 +0200 | [diff] [blame] | 1218 | .port_probe = iuu_create_sysfs_attrs, |
| 1219 | .port_remove = iuu_remove_sysfs_attrs, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1220 | .open = iuu_open, |
| 1221 | .close = iuu_close, |
| 1222 | .write = iuu_uart_write, |
| 1223 | .read_bulk_callback = iuu_uart_read_callback, |
| 1224 | .tiocmget = iuu_tiocmget, |
| 1225 | .tiocmset = iuu_tiocmset, |
Olivier Bornet | 96dab77 | 2009-06-11 12:54:20 +0100 | [diff] [blame] | 1226 | .set_termios = iuu_set_termios, |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1227 | .init_termios = iuu_init_termios, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1228 | .attach = iuu_startup, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 1229 | .release = iuu_release, |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1230 | }; |
| 1231 | |
Alan Stern | 7dbe246 | 2012-02-23 14:56:57 -0500 | [diff] [blame] | 1232 | static struct usb_serial_driver * const serial_drivers[] = { |
| 1233 | &iuu_device, NULL |
| 1234 | }; |
| 1235 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 1236 | module_usb_serial_driver(serial_drivers, id_table); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1237 | |
| 1238 | MODULE_AUTHOR("Alain Degreffe eczema@ecze.com"); |
| 1239 | |
| 1240 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1241 | MODULE_LICENSE("GPL"); |
| 1242 | |
| 1243 | MODULE_VERSION(DRIVER_VERSION); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1244 | |
| 1245 | module_param(xmas, bool, S_IRUGO | S_IWUSR); |
Olivier Bornet | 8844a32 | 2009-08-18 21:05:54 +0200 | [diff] [blame] | 1246 | MODULE_PARM_DESC(xmas, "Xmas colors enabled or not"); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1247 | |
| 1248 | module_param(boost, int, S_IRUGO | S_IWUSR); |
Olivier Bornet | 8844a32 | 2009-08-18 21:05:54 +0200 | [diff] [blame] | 1249 | MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)"); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1250 | |
| 1251 | module_param(clockmode, int, S_IRUGO | S_IWUSR); |
Olivier Bornet | 8844a32 | 2009-08-18 21:05:54 +0200 | [diff] [blame] | 1252 | MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, " |
| 1253 | "3=6 Mhz)"); |
Alain Degreffe | 60a8fc0 | 2007-10-26 13:51:49 +0200 | [diff] [blame] | 1254 | |
| 1255 | module_param(cdmode, int, S_IRUGO | S_IWUSR); |
Olivier Bornet | 8844a32 | 2009-08-18 21:05:54 +0200 | [diff] [blame] | 1256 | MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, " |
| 1257 | "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)"); |
Olivier Bornet | e55c6d0 | 2009-08-18 21:05:57 +0200 | [diff] [blame] | 1258 | |
| 1259 | module_param(vcc_default, int, S_IRUGO | S_IWUSR); |
| 1260 | MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 " |
| 1261 | "for 5V). Default to 5."); |