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