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