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