Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mos7720.c |
| 3 | * Controls the Moschip 7720 usb to dual port serial convertor |
| 4 | * |
| 5 | * Copyright 2006 Moschip Semiconductor Tech. Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, version 2 of the License. |
| 10 | * |
| 11 | * Developed by: |
Greg Kroah-Hartman | 50d2dc7 | 2007-06-25 01:08:01 -0700 | [diff] [blame] | 12 | * Vijaya Kumar <vijaykumar.gn@gmail.com> |
| 13 | * Ajay Kumar <naanuajay@yahoo.com> |
| 14 | * Gurudeva <ngurudeva@yahoo.com> |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 15 | * |
| 16 | * Cleaned up from the original by: |
| 17 | * Greg Kroah-Hartman <gregkh@suse.de> |
| 18 | * |
| 19 | * Originally based on drivers/usb/serial/io_edgeport.c which is: |
| 20 | * Copyright (C) 2000 Inside Out Networks, All rights reserved. |
| 21 | * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> |
| 22 | */ |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/tty.h> |
| 28 | #include <linux/tty_driver.h> |
| 29 | #include <linux/tty_flip.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/serial.h> |
| 33 | #include <linux/serial_reg.h> |
| 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb/serial.h> |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /* |
| 40 | * Version Information |
| 41 | */ |
| 42 | #define DRIVER_VERSION "1.0.0.4F" |
| 43 | #define DRIVER_AUTHOR "Aspire Communications pvt Ltd." |
| 44 | #define DRIVER_DESC "Moschip USB Serial Driver" |
| 45 | |
| 46 | /* default urb timeout */ |
| 47 | #define MOS_WDR_TIMEOUT (HZ * 5) |
| 48 | |
| 49 | #define MOS_PORT1 0x0200 |
| 50 | #define MOS_PORT2 0x0300 |
| 51 | #define MOS_VENREG 0x0000 |
| 52 | #define MOS_MAX_PORT 0x02 |
| 53 | #define MOS_WRITE 0x0E |
| 54 | #define MOS_READ 0x0D |
| 55 | |
| 56 | /* Interrupt Rotinue Defines */ |
| 57 | #define SERIAL_IIR_RLS 0x06 |
| 58 | #define SERIAL_IIR_RDA 0x04 |
| 59 | #define SERIAL_IIR_CTI 0x0c |
| 60 | #define SERIAL_IIR_THR 0x02 |
| 61 | #define SERIAL_IIR_MS 0x00 |
| 62 | |
| 63 | #define NUM_URBS 16 /* URB Count */ |
| 64 | #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ |
| 65 | |
| 66 | /* This structure holds all of the local port information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 67 | struct moschip_port { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 68 | __u8 shadowLCR; /* last LCR value received */ |
| 69 | __u8 shadowMCR; /* last MCR value received */ |
| 70 | __u8 shadowMSR; /* last MSR value received */ |
| 71 | char open; |
| 72 | struct async_icount icount; |
| 73 | struct usb_serial_port *port; /* loop back to the owner */ |
| 74 | struct urb *write_urb_pool[NUM_URBS]; |
| 75 | }; |
| 76 | |
| 77 | /* This structure holds all of the individual serial device information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 78 | struct moschip_serial { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 79 | int interrupt_started; |
| 80 | }; |
| 81 | |
| 82 | static int debug; |
| 83 | |
| 84 | #define USB_VENDOR_ID_MOSCHIP 0x9710 |
| 85 | #define MOSCHIP_DEVICE_ID_7720 0x7720 |
| 86 | #define MOSCHIP_DEVICE_ID_7715 0x7715 |
| 87 | |
| 88 | static struct usb_device_id moschip_port_id_table [] = { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 89 | { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) }, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 90 | { } /* terminating entry */ |
| 91 | }; |
| 92 | MODULE_DEVICE_TABLE(usb, moschip_port_id_table); |
| 93 | |
| 94 | |
| 95 | /* |
| 96 | * mos7720_interrupt_callback |
| 97 | * this is the callback function for when we have received data on the |
| 98 | * interrupt endpoint. |
| 99 | */ |
| 100 | static void mos7720_interrupt_callback(struct urb *urb) |
| 101 | { |
| 102 | int result; |
| 103 | int length; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 104 | int status = urb->status; |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 105 | __u8 *data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 106 | __u8 sp1; |
| 107 | __u8 sp2; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 108 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 109 | dbg("%s", " : Entering\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 110 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 111 | switch (status) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 112 | case 0: |
| 113 | /* success */ |
| 114 | break; |
| 115 | case -ECONNRESET: |
| 116 | case -ENOENT: |
| 117 | case -ESHUTDOWN: |
| 118 | /* this urb is terminated, clean up */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 119 | dbg("%s - urb shutting down with status: %d", __func__, |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 120 | status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 121 | return; |
| 122 | default: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 123 | dbg("%s - nonzero urb status received: %d", __func__, |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 124 | status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 125 | goto exit; |
| 126 | } |
| 127 | |
| 128 | length = urb->actual_length; |
| 129 | data = urb->transfer_buffer; |
| 130 | |
| 131 | /* Moschip get 4 bytes |
| 132 | * Byte 1 IIR Port 1 (port.number is 0) |
| 133 | * Byte 2 IIR Port 2 (port.number is 1) |
| 134 | * Byte 3 -------------- |
| 135 | * Byte 4 FIFO status for both */ |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 136 | |
| 137 | /* the above description is inverted |
| 138 | * oneukum 2007-03-14 */ |
| 139 | |
| 140 | if (unlikely(length != 4)) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 141 | dbg("Wrong data !!!"); |
| 142 | return; |
| 143 | } |
| 144 | |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 145 | sp1 = data[3]; |
| 146 | sp2 = data[2]; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 147 | |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 148 | if ((sp1 | sp2) & 0x01) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 149 | /* No Interrupt Pending in both the ports */ |
| 150 | dbg("No Interrupt !!!"); |
| 151 | } else { |
| 152 | switch (sp1 & 0x0f) { |
| 153 | case SERIAL_IIR_RLS: |
| 154 | dbg("Serial Port 1: Receiver status error or address " |
| 155 | "bit detected in 9-bit mode\n"); |
| 156 | break; |
| 157 | case SERIAL_IIR_CTI: |
| 158 | dbg("Serial Port 1: Receiver time out"); |
| 159 | break; |
| 160 | case SERIAL_IIR_MS: |
| 161 | dbg("Serial Port 1: Modem status change"); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | switch (sp2 & 0x0f) { |
| 166 | case SERIAL_IIR_RLS: |
| 167 | dbg("Serial Port 2: Receiver status error or address " |
| 168 | "bit detected in 9-bit mode"); |
| 169 | break; |
| 170 | case SERIAL_IIR_CTI: |
| 171 | dbg("Serial Port 2: Receiver time out"); |
| 172 | break; |
| 173 | case SERIAL_IIR_MS: |
| 174 | dbg("Serial Port 2: Modem status change"); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | exit: |
| 180 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 181 | if (result) |
| 182 | dev_err(&urb->dev->dev, |
| 183 | "%s - Error %d submitting control urb\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 184 | __func__, result); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * mos7720_bulk_in_callback |
| 190 | * this is the callback function for when we have received data on the |
| 191 | * bulk in endpoint. |
| 192 | */ |
| 193 | static void mos7720_bulk_in_callback(struct urb *urb) |
| 194 | { |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 195 | int retval; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 196 | unsigned char *data ; |
| 197 | struct usb_serial_port *port; |
| 198 | struct moschip_port *mos7720_port; |
| 199 | struct tty_struct *tty; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 200 | int status = urb->status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 201 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 202 | if (status) { |
| 203 | dbg("nonzero read bulk status received: %d", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
| 207 | mos7720_port = urb->context; |
| 208 | if (!mos7720_port) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 209 | dbg("%s", "NULL mos7720_port pointer \n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 210 | return ; |
| 211 | } |
| 212 | |
| 213 | port = mos7720_port->port; |
| 214 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 215 | dbg("Entering...%s", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 216 | |
| 217 | data = urb->transfer_buffer; |
| 218 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 219 | tty = tty_port_tty_get(&port->port); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 220 | if (tty && urb->actual_length) { |
| 221 | tty_buffer_request_room(tty, urb->actual_length); |
| 222 | tty_insert_flip_string(tty, data, urb->actual_length); |
| 223 | tty_flip_buffer_push(tty); |
| 224 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 225 | tty_kref_put(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 226 | |
| 227 | if (!port->read_urb) { |
| 228 | dbg("URB KILLED !!!"); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if (port->read_urb->status != -EINPROGRESS) { |
| 233 | port->read_urb->dev = port->serial->dev; |
| 234 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 235 | retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 236 | if (retval) |
| 237 | dbg("usb_submit_urb(read bulk) failed, retval = %d", |
| 238 | retval); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * mos7720_bulk_out_data_callback |
| 244 | * this is the callback function for when we have finished sending serial |
| 245 | * data on the bulk out endpoint. |
| 246 | */ |
| 247 | static void mos7720_bulk_out_data_callback(struct urb *urb) |
| 248 | { |
| 249 | struct moschip_port *mos7720_port; |
| 250 | struct tty_struct *tty; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 251 | int status = urb->status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 252 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 253 | if (status) { |
| 254 | dbg("nonzero write bulk status received:%d", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | |
| 258 | mos7720_port = urb->context; |
| 259 | if (!mos7720_port) { |
| 260 | dbg("NULL mos7720_port pointer"); |
| 261 | return ; |
| 262 | } |
| 263 | |
| 264 | dbg("Entering ........."); |
| 265 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 266 | tty = tty_port_tty_get(&mos7720_port->port->port); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 267 | |
Jiri Slaby | b963a84 | 2007-02-10 01:44:55 -0800 | [diff] [blame] | 268 | if (tty && mos7720_port->open) |
| 269 | tty_wakeup(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 270 | tty_kref_put(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * send_mos_cmd |
| 275 | * this function will be used for sending command to device |
| 276 | */ |
| 277 | static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value, |
| 278 | __u16 index, void *data) |
| 279 | { |
| 280 | int status; |
| 281 | unsigned int pipe; |
| 282 | u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); |
| 283 | __u8 requesttype; |
| 284 | __u16 size = 0x0000; |
| 285 | |
| 286 | if (value < MOS_MAX_PORT) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 287 | if (product == MOSCHIP_DEVICE_ID_7715) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 288 | value = value*0x100+0x100; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 289 | else |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 290 | value = value*0x100+0x200; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 291 | } else { |
| 292 | value = 0x0000; |
| 293 | if ((product == MOSCHIP_DEVICE_ID_7715) && |
| 294 | (index != 0x08)) { |
| 295 | dbg("serial->product== MOSCHIP_DEVICE_ID_7715"); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 296 | /* index = 0x01 ; */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | if (request == MOS_WRITE) { |
| 301 | request = (__u8)MOS_WRITE; |
| 302 | requesttype = (__u8)0x40; |
| 303 | value = value + (__u16)*((unsigned char *)data); |
| 304 | data = NULL; |
| 305 | pipe = usb_sndctrlpipe(serial->dev, 0); |
| 306 | } else { |
| 307 | request = (__u8)MOS_READ; |
| 308 | requesttype = (__u8)0xC0; |
| 309 | size = 0x01; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 310 | pipe = usb_rcvctrlpipe(serial->dev, 0); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | status = usb_control_msg(serial->dev, pipe, request, requesttype, |
| 314 | value, index, data, size, MOS_WDR_TIMEOUT); |
| 315 | |
| 316 | if (status < 0) |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 317 | dbg("Command Write failed Value %x index %x\n", value, index); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 318 | |
| 319 | return status; |
| 320 | } |
| 321 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 322 | static int mos7720_open(struct tty_struct *tty, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 323 | struct usb_serial_port *port, struct file *filp) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 324 | { |
| 325 | struct usb_serial *serial; |
| 326 | struct usb_serial_port *port0; |
| 327 | struct urb *urb; |
| 328 | struct moschip_serial *mos7720_serial; |
| 329 | struct moschip_port *mos7720_port; |
| 330 | int response; |
| 331 | int port_number; |
| 332 | char data; |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 333 | int allocated_urbs = 0; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 334 | int j; |
| 335 | |
| 336 | serial = port->serial; |
| 337 | |
| 338 | mos7720_port = usb_get_serial_port_data(port); |
| 339 | if (mos7720_port == NULL) |
| 340 | return -ENODEV; |
| 341 | |
| 342 | port0 = serial->port[0]; |
| 343 | |
| 344 | mos7720_serial = usb_get_serial_data(serial); |
| 345 | |
| 346 | if (mos7720_serial == NULL || port0 == NULL) |
| 347 | return -ENODEV; |
| 348 | |
| 349 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 350 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 351 | |
| 352 | /* Initialising the write urb pool */ |
| 353 | for (j = 0; j < NUM_URBS; ++j) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 354 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 355 | mos7720_port->write_urb_pool[j] = urb; |
| 356 | |
| 357 | if (urb == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 358 | dev_err(&port->dev, "No more urbs???\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 359 | continue; |
| 360 | } |
| 361 | |
| 362 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 363 | GFP_KERNEL); |
| 364 | if (!urb->transfer_buffer) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 365 | dev_err(&port->dev, |
| 366 | "%s-out of memory for urb buffers.\n", |
| 367 | __func__); |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 368 | usb_free_urb(mos7720_port->write_urb_pool[j]); |
| 369 | mos7720_port->write_urb_pool[j] = NULL; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 370 | continue; |
| 371 | } |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 372 | allocated_urbs++; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 375 | if (!allocated_urbs) |
| 376 | return -ENOMEM; |
| 377 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 378 | /* Initialize MCS7720 -- Write Init values to corresponding Registers |
| 379 | * |
| 380 | * Register Index |
| 381 | * 1 : IER |
| 382 | * 2 : FCR |
| 383 | * 3 : LCR |
| 384 | * 4 : MCR |
| 385 | * |
| 386 | * 0x08 : SP1/2 Control Reg |
| 387 | */ |
| 388 | port_number = port->number - port->serial->minor; |
| 389 | send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 390 | dbg("SS::%p LSR:%x\n", mos7720_port, data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 391 | |
| 392 | dbg("Check:Sending Command .........."); |
| 393 | |
| 394 | data = 0x02; |
| 395 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data); |
| 396 | data = 0x02; |
| 397 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data); |
| 398 | |
| 399 | data = 0x00; |
| 400 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
| 401 | data = 0x00; |
| 402 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data); |
| 403 | |
| 404 | data = 0xCF; |
| 405 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data); |
| 406 | data = 0x03; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 407 | mos7720_port->shadowLCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 408 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data); |
| 409 | data = 0x0b; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 410 | mos7720_port->shadowMCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 411 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 412 | data = 0x0b; |
| 413 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 414 | |
| 415 | data = 0x00; |
| 416 | send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data); |
| 417 | data = 0x00; |
| 418 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data); |
| 419 | |
| 420 | /* data = 0x00; |
| 421 | send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data); |
| 422 | data = 0x03; |
| 423 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data); |
| 424 | data = 0x00; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 425 | send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, |
| 426 | port_number + 1, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 427 | */ |
| 428 | data = 0x00; |
| 429 | send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data); |
| 430 | |
| 431 | data = data | (port->number - port->serial->minor + 1); |
| 432 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data); |
| 433 | |
| 434 | data = 0x83; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 435 | mos7720_port->shadowLCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 436 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data); |
| 437 | data = 0x0c; |
| 438 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data); |
| 439 | data = 0x00; |
| 440 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
| 441 | data = 0x03; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 442 | mos7720_port->shadowLCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 443 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data); |
| 444 | data = 0x0c; |
| 445 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
| 446 | data = 0x0c; |
| 447 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
| 448 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 449 | /* force low_latency on so that our tty_push actually forces * |
| 450 | * the data through,otherwise it is scheduled, and with * |
| 451 | * high data rates (like with OHCI) data can get lost. */ |
| 452 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 453 | if (tty) |
| 454 | tty->low_latency = 1; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 455 | |
| 456 | /* see if we've set up our endpoint info yet * |
| 457 | * (can't set it up in mos7720_startup as the * |
| 458 | * structures were not set up at that time.) */ |
| 459 | if (!mos7720_serial->interrupt_started) { |
| 460 | dbg("Interrupt buffer NULL !!!"); |
| 461 | |
| 462 | /* not set up yet, so do it now */ |
| 463 | mos7720_serial->interrupt_started = 1; |
| 464 | |
| 465 | dbg("To Submit URB !!!"); |
| 466 | |
| 467 | /* set up our interrupt urb */ |
| 468 | usb_fill_int_urb(port0->interrupt_in_urb, serial->dev, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 469 | usb_rcvintpipe(serial->dev, |
| 470 | port->interrupt_in_endpointAddress), |
| 471 | port0->interrupt_in_buffer, |
| 472 | port0->interrupt_in_urb->transfer_buffer_length, |
| 473 | mos7720_interrupt_callback, mos7720_port, |
| 474 | port0->interrupt_in_urb->interval); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 475 | |
| 476 | /* start interrupt read for this mos7720 this interrupt * |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 477 | * will continue as long as the mos7720 is connected */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 478 | dbg("Submit URB over !!!"); |
| 479 | response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL); |
| 480 | if (response) |
| 481 | dev_err(&port->dev, |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 482 | "%s - Error %d submitting control urb\n", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 483 | __func__, response); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* set up our bulk in urb */ |
| 487 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
| 488 | usb_rcvbulkpipe(serial->dev, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 489 | port->bulk_in_endpointAddress), |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 490 | port->bulk_in_buffer, |
| 491 | port->read_urb->transfer_buffer_length, |
| 492 | mos7720_bulk_in_callback, mos7720_port); |
| 493 | response = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 494 | if (response) |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 495 | dev_err(&port->dev, "%s - Error %d submitting read urb\n", |
| 496 | __func__, response); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 497 | |
| 498 | /* initialize our icount structure */ |
| 499 | memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount)); |
| 500 | |
| 501 | /* initialize our port settings */ |
| 502 | mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */ |
| 503 | |
| 504 | /* send a open port command */ |
| 505 | mos7720_port->open = 1; |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * mos7720_chars_in_buffer |
| 512 | * this function is called by the tty driver when it wants to know how many |
| 513 | * bytes of data we currently have outstanding in the port (data that has |
| 514 | * been written, but hasn't made it out the port yet) |
| 515 | * If successful, we return the number of bytes left to be written in the |
| 516 | * system, |
| 517 | * Otherwise we return a negative error number. |
| 518 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 519 | static int mos7720_chars_in_buffer(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 520 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 521 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 522 | int i; |
| 523 | int chars = 0; |
| 524 | struct moschip_port *mos7720_port; |
| 525 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 526 | dbg("%s:entering ...........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 527 | |
| 528 | mos7720_port = usb_get_serial_port_data(port); |
| 529 | if (mos7720_port == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 530 | dbg("%s:leaving ...........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 531 | return -ENODEV; |
| 532 | } |
| 533 | |
| 534 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 535 | if (mos7720_port->write_urb_pool[i] && |
| 536 | mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 537 | chars += URB_TRANSFER_BUFFER_SIZE; |
| 538 | } |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 539 | dbg("%s - returns %d", __func__, chars); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 540 | return chars; |
| 541 | } |
| 542 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 543 | static void mos7720_close(struct tty_struct *tty, |
| 544 | struct usb_serial_port *port, struct file *filp) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 545 | { |
| 546 | struct usb_serial *serial; |
| 547 | struct moschip_port *mos7720_port; |
| 548 | char data; |
| 549 | int j; |
| 550 | |
| 551 | dbg("mos7720_close:entering..."); |
| 552 | |
| 553 | serial = port->serial; |
| 554 | |
| 555 | mos7720_port = usb_get_serial_port_data(port); |
| 556 | if (mos7720_port == NULL) |
| 557 | return; |
| 558 | |
| 559 | for (j = 0; j < NUM_URBS; ++j) |
| 560 | usb_kill_urb(mos7720_port->write_urb_pool[j]); |
| 561 | |
| 562 | /* Freeing Write URBs */ |
| 563 | for (j = 0; j < NUM_URBS; ++j) { |
| 564 | if (mos7720_port->write_urb_pool[j]) { |
| 565 | kfree(mos7720_port->write_urb_pool[j]->transfer_buffer); |
| 566 | usb_free_urb(mos7720_port->write_urb_pool[j]); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* While closing port, shutdown all bulk read, write * |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 571 | * and interrupt read if they exists, otherwise nop */ |
| 572 | dbg("Shutdown bulk write"); |
| 573 | usb_kill_urb(port->write_urb); |
| 574 | dbg("Shutdown bulk read"); |
| 575 | usb_kill_urb(port->read_urb); |
| 576 | |
| 577 | mutex_lock(&serial->disc_mutex); |
| 578 | /* these commands must not be issued if the device has |
| 579 | * been disconnected */ |
| 580 | if (!serial->disconnected) { |
| 581 | data = 0x00; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 582 | send_mos_cmd(serial, MOS_WRITE, |
| 583 | port->number - port->serial->minor, 0x04, &data); |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 584 | |
| 585 | data = 0x00; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 586 | send_mos_cmd(serial, MOS_WRITE, |
| 587 | port->number - port->serial->minor, 0x01, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 588 | } |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 589 | mutex_unlock(&serial->disc_mutex); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 590 | mos7720_port->open = 0; |
| 591 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 592 | dbg("Leaving %s", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 595 | static void mos7720_break(struct tty_struct *tty, int break_state) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 596 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 597 | struct usb_serial_port *port = tty->driver_data; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 598 | unsigned char data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 599 | struct usb_serial *serial; |
| 600 | struct moschip_port *mos7720_port; |
| 601 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 602 | dbg("Entering %s", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 603 | |
| 604 | serial = port->serial; |
| 605 | |
| 606 | mos7720_port = usb_get_serial_port_data(port); |
| 607 | if (mos7720_port == NULL) |
| 608 | return; |
| 609 | |
| 610 | if (break_state == -1) |
| 611 | data = mos7720_port->shadowLCR | UART_LCR_SBC; |
| 612 | else |
| 613 | data = mos7720_port->shadowLCR & ~UART_LCR_SBC; |
| 614 | |
| 615 | mos7720_port->shadowLCR = data; |
| 616 | send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor, |
| 617 | 0x03, &data); |
| 618 | |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * mos7720_write_room |
| 624 | * this function is called by the tty driver when it wants to know how many |
| 625 | * bytes of data we can accept for a specific port. |
| 626 | * If successful, we return the amount of room that we have for this port |
| 627 | * Otherwise we return a negative error number. |
| 628 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 629 | static int mos7720_write_room(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 630 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 631 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 632 | struct moschip_port *mos7720_port; |
| 633 | int room = 0; |
| 634 | int i; |
| 635 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 636 | dbg("%s:entering ...........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 637 | |
| 638 | mos7720_port = usb_get_serial_port_data(port); |
| 639 | if (mos7720_port == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 640 | dbg("%s:leaving ...........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 641 | return -ENODEV; |
| 642 | } |
| 643 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 644 | /* FIXME: Locking */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 645 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 646 | if (mos7720_port->write_urb_pool[i] && |
| 647 | mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 648 | room += URB_TRANSFER_BUFFER_SIZE; |
| 649 | } |
| 650 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 651 | dbg("%s - returns %d", __func__, room); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 652 | return room; |
| 653 | } |
| 654 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 655 | static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 656 | const unsigned char *data, int count) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 657 | { |
| 658 | int status; |
| 659 | int i; |
| 660 | int bytes_sent = 0; |
| 661 | int transfer_size; |
| 662 | |
| 663 | struct moschip_port *mos7720_port; |
| 664 | struct usb_serial *serial; |
| 665 | struct urb *urb; |
| 666 | const unsigned char *current_position = data; |
| 667 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 668 | dbg("%s:entering ...........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 669 | |
| 670 | serial = port->serial; |
| 671 | |
| 672 | mos7720_port = usb_get_serial_port_data(port); |
| 673 | if (mos7720_port == NULL) { |
| 674 | dbg("mos7720_port is NULL"); |
| 675 | return -ENODEV; |
| 676 | } |
| 677 | |
| 678 | /* try to find a free urb in the list */ |
| 679 | urb = NULL; |
| 680 | |
| 681 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 682 | if (mos7720_port->write_urb_pool[i] && |
| 683 | mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 684 | urb = mos7720_port->write_urb_pool[i]; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 685 | dbg("URB:%d", i); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (urb == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 691 | dbg("%s - no more free urbs", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 692 | goto exit; |
| 693 | } |
| 694 | |
| 695 | if (urb->transfer_buffer == NULL) { |
| 696 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 697 | GFP_KERNEL); |
| 698 | if (urb->transfer_buffer == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 699 | dev_err(&port->dev, "%s no more kernel memory...\n", |
| 700 | __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 701 | goto exit; |
| 702 | } |
| 703 | } |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 704 | transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 705 | |
| 706 | memcpy(urb->transfer_buffer, current_position, transfer_size); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 707 | usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 708 | urb->transfer_buffer); |
| 709 | |
| 710 | /* fill urb with data and submit */ |
| 711 | usb_fill_bulk_urb(urb, serial->dev, |
| 712 | usb_sndbulkpipe(serial->dev, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 713 | port->bulk_out_endpointAddress), |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 714 | urb->transfer_buffer, transfer_size, |
| 715 | mos7720_bulk_out_data_callback, mos7720_port); |
| 716 | |
| 717 | /* send it down the pipe */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 718 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 719 | if (status) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 720 | dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed " |
| 721 | "with status = %d\n", __func__, status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 722 | bytes_sent = status; |
| 723 | goto exit; |
| 724 | } |
| 725 | bytes_sent = transfer_size; |
| 726 | |
| 727 | exit: |
| 728 | return bytes_sent; |
| 729 | } |
| 730 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 731 | static void mos7720_throttle(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 732 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 733 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 734 | struct moschip_port *mos7720_port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 735 | int status; |
| 736 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 737 | dbg("%s- port %d\n", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 738 | |
| 739 | mos7720_port = usb_get_serial_port_data(port); |
| 740 | |
| 741 | if (mos7720_port == NULL) |
| 742 | return; |
| 743 | |
| 744 | if (!mos7720_port->open) { |
| 745 | dbg("port not opened"); |
| 746 | return; |
| 747 | } |
| 748 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 749 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 750 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 751 | /* if we are implementing XON/XOFF, send the stop character */ |
| 752 | if (I_IXOFF(tty)) { |
| 753 | unsigned char stop_char = STOP_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 754 | status = mos7720_write(tty, port, &stop_char, 1); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 755 | if (status <= 0) |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | /* if we are implementing RTS/CTS, toggle that line */ |
| 760 | if (tty->termios->c_cflag & CRTSCTS) { |
| 761 | mos7720_port->shadowMCR &= ~UART_MCR_RTS; |
| 762 | status = send_mos_cmd(port->serial, MOS_WRITE, |
| 763 | port->number - port->serial->minor, |
| 764 | UART_MCR, &mos7720_port->shadowMCR); |
| 765 | if (status != 0) |
| 766 | return; |
| 767 | } |
| 768 | } |
| 769 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 770 | static void mos7720_unthrottle(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 771 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 772 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 773 | struct moschip_port *mos7720_port = usb_get_serial_port_data(port); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 774 | int status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 775 | |
| 776 | if (mos7720_port == NULL) |
| 777 | return; |
| 778 | |
| 779 | if (!mos7720_port->open) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 780 | dbg("%s - port not opened", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 781 | return; |
| 782 | } |
| 783 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 784 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 785 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 786 | /* if we are implementing XON/XOFF, send the start character */ |
| 787 | if (I_IXOFF(tty)) { |
| 788 | unsigned char start_char = START_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 789 | status = mos7720_write(tty, port, &start_char, 1); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 790 | if (status <= 0) |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | /* if we are implementing RTS/CTS, toggle that line */ |
| 795 | if (tty->termios->c_cflag & CRTSCTS) { |
| 796 | mos7720_port->shadowMCR |= UART_MCR_RTS; |
| 797 | status = send_mos_cmd(port->serial, MOS_WRITE, |
| 798 | port->number - port->serial->minor, |
| 799 | UART_MCR, &mos7720_port->shadowMCR); |
| 800 | if (status != 0) |
| 801 | return; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | static int set_higher_rates(struct moschip_port *mos7720_port, |
| 806 | unsigned int baud) |
| 807 | { |
| 808 | unsigned char data; |
| 809 | struct usb_serial_port *port; |
| 810 | struct usb_serial *serial; |
| 811 | int port_number; |
| 812 | |
| 813 | if (mos7720_port == NULL) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | port = mos7720_port->port; |
| 817 | serial = port->serial; |
| 818 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 819 | /*********************************************** |
| 820 | * Init Sequence for higher rates |
| 821 | ***********************************************/ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 822 | dbg("Sending Setting Commands .........."); |
| 823 | port_number = port->number - port->serial->minor; |
| 824 | |
| 825 | data = 0x000; |
| 826 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
| 827 | data = 0x000; |
| 828 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data); |
| 829 | data = 0x0CF; |
| 830 | send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data); |
| 831 | data = 0x00b; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 832 | mos7720_port->shadowMCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 833 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 834 | data = 0x00b; |
| 835 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 836 | |
| 837 | data = 0x000; |
| 838 | send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data); |
| 839 | data = 0x000; |
| 840 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data); |
| 841 | |
| 842 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 843 | /*********************************************** |
| 844 | * Set for higher rates * |
| 845 | ***********************************************/ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 846 | |
| 847 | data = baud * 0x10; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 848 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 849 | |
| 850 | data = 0x003; |
| 851 | send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data); |
| 852 | data = 0x003; |
| 853 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data); |
| 854 | |
| 855 | data = 0x02b; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 856 | mos7720_port->shadowMCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 857 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 858 | data = 0x02b; |
| 859 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 860 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 861 | /*********************************************** |
| 862 | * Set DLL/DLM |
| 863 | ***********************************************/ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 864 | |
| 865 | data = mos7720_port->shadowLCR | UART_LCR_DLAB; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 866 | mos7720_port->shadowLCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 867 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data); |
| 868 | |
| 869 | data = 0x001; /* DLL */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 870 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 871 | data = 0x000; /* DLM */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 872 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 873 | |
| 874 | data = mos7720_port->shadowLCR & ~UART_LCR_DLAB; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 875 | mos7720_port->shadowLCR = data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 876 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* baud rate information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 882 | struct divisor_table_entry { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 883 | __u32 baudrate; |
| 884 | __u16 divisor; |
| 885 | }; |
| 886 | |
| 887 | /* Define table of divisors for moschip 7720 hardware * |
| 888 | * These assume a 3.6864MHz crystal, the standard /16, and * |
| 889 | * MCR.7 = 0. */ |
| 890 | static struct divisor_table_entry divisor_table[] = { |
| 891 | { 50, 2304}, |
| 892 | { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */ |
| 893 | { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */ |
| 894 | { 150, 768}, |
| 895 | { 300, 384}, |
| 896 | { 600, 192}, |
| 897 | { 1200, 96}, |
| 898 | { 1800, 64}, |
| 899 | { 2400, 48}, |
| 900 | { 4800, 24}, |
| 901 | { 7200, 16}, |
| 902 | { 9600, 12}, |
| 903 | { 19200, 6}, |
| 904 | { 38400, 3}, |
| 905 | { 57600, 2}, |
| 906 | { 115200, 1}, |
| 907 | }; |
| 908 | |
| 909 | /***************************************************************************** |
| 910 | * calc_baud_rate_divisor |
| 911 | * this function calculates the proper baud rate divisor for the specified |
| 912 | * baud rate. |
| 913 | *****************************************************************************/ |
| 914 | static int calc_baud_rate_divisor(int baudrate, int *divisor) |
| 915 | { |
| 916 | int i; |
| 917 | __u16 custom; |
| 918 | __u16 round1; |
| 919 | __u16 round; |
| 920 | |
| 921 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 922 | dbg("%s - %d", __func__, baudrate); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 923 | |
| 924 | for (i = 0; i < ARRAY_SIZE(divisor_table); i++) { |
| 925 | if (divisor_table[i].baudrate == baudrate) { |
| 926 | *divisor = divisor_table[i].divisor; |
| 927 | return 0; |
| 928 | } |
| 929 | } |
| 930 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 931 | /* After trying for all the standard baud rates * |
| 932 | * Try calculating the divisor for this baud rate */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 933 | if (baudrate > 75 && baudrate < 230400) { |
| 934 | /* get the divisor */ |
| 935 | custom = (__u16)(230400L / baudrate); |
| 936 | |
| 937 | /* Check for round off */ |
| 938 | round1 = (__u16)(2304000L / baudrate); |
| 939 | round = (__u16)(round1 - (custom * 10)); |
| 940 | if (round > 4) |
| 941 | custom++; |
| 942 | *divisor = custom; |
| 943 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 944 | dbg("Baud %d = %d", baudrate, custom); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | dbg("Baud calculation Failed..."); |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * send_cmd_write_baud_rate |
| 954 | * this function sends the proper command to change the baud rate of the |
| 955 | * specified port. |
| 956 | */ |
| 957 | static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port, |
| 958 | int baudrate) |
| 959 | { |
| 960 | struct usb_serial_port *port; |
| 961 | struct usb_serial *serial; |
| 962 | int divisor; |
| 963 | int status; |
| 964 | unsigned char data; |
| 965 | unsigned char number; |
| 966 | |
| 967 | if (mos7720_port == NULL) |
| 968 | return -1; |
| 969 | |
| 970 | port = mos7720_port->port; |
| 971 | serial = port->serial; |
| 972 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 973 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 974 | |
| 975 | number = port->number - port->serial->minor; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 976 | dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 977 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 978 | /* Calculate the Divisor */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 979 | status = calc_baud_rate_divisor(baudrate, &divisor); |
| 980 | if (status) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 981 | dev_err(&port->dev, "%s - bad baud rate\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 982 | return status; |
| 983 | } |
| 984 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 985 | /* Enable access to divisor latch */ |
| 986 | data = mos7720_port->shadowLCR | UART_LCR_DLAB; |
| 987 | mos7720_port->shadowLCR = data; |
| 988 | send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 989 | |
| 990 | /* Write the divisor */ |
| 991 | data = ((unsigned char)(divisor & 0xff)); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 992 | send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 993 | |
| 994 | data = ((unsigned char)((divisor & 0xff00) >> 8)); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 995 | send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 996 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 997 | /* Disable access to divisor latch */ |
| 998 | data = mos7720_port->shadowLCR & ~UART_LCR_DLAB; |
| 999 | mos7720_port->shadowLCR = data; |
| 1000 | send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1001 | |
| 1002 | return status; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * change_port_settings |
| 1007 | * This routine is called to set the UART on the device to match |
| 1008 | * the specified new settings. |
| 1009 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1010 | static void change_port_settings(struct tty_struct *tty, |
| 1011 | struct moschip_port *mos7720_port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1012 | struct ktermios *old_termios) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct usb_serial_port *port; |
| 1015 | struct usb_serial *serial; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1016 | int baud; |
| 1017 | unsigned cflag; |
| 1018 | unsigned iflag; |
| 1019 | __u8 mask = 0xff; |
| 1020 | __u8 lData; |
| 1021 | __u8 lParity; |
| 1022 | __u8 lStop; |
| 1023 | int status; |
| 1024 | int port_number; |
| 1025 | char data; |
| 1026 | |
| 1027 | if (mos7720_port == NULL) |
| 1028 | return ; |
| 1029 | |
| 1030 | port = mos7720_port->port; |
| 1031 | serial = port->serial; |
| 1032 | port_number = port->number - port->serial->minor; |
| 1033 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1034 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1035 | |
| 1036 | if (!mos7720_port->open) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1037 | dbg("%s - port not opened", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1038 | return; |
| 1039 | } |
| 1040 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1041 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1042 | |
| 1043 | lData = UART_LCR_WLEN8; |
| 1044 | lStop = 0x00; /* 1 stop bit */ |
| 1045 | lParity = 0x00; /* No parity */ |
| 1046 | |
| 1047 | cflag = tty->termios->c_cflag; |
| 1048 | iflag = tty->termios->c_iflag; |
| 1049 | |
| 1050 | /* Change the number of bits */ |
| 1051 | switch (cflag & CSIZE) { |
| 1052 | case CS5: |
| 1053 | lData = UART_LCR_WLEN5; |
| 1054 | mask = 0x1f; |
| 1055 | break; |
| 1056 | |
| 1057 | case CS6: |
| 1058 | lData = UART_LCR_WLEN6; |
| 1059 | mask = 0x3f; |
| 1060 | break; |
| 1061 | |
| 1062 | case CS7: |
| 1063 | lData = UART_LCR_WLEN7; |
| 1064 | mask = 0x7f; |
| 1065 | break; |
| 1066 | default: |
| 1067 | case CS8: |
| 1068 | lData = UART_LCR_WLEN8; |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | /* Change the Parity bit */ |
| 1073 | if (cflag & PARENB) { |
| 1074 | if (cflag & PARODD) { |
| 1075 | lParity = UART_LCR_PARITY; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1076 | dbg("%s - parity = odd", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1077 | } else { |
| 1078 | lParity = (UART_LCR_EPAR | UART_LCR_PARITY); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1079 | dbg("%s - parity = even", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | } else { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1083 | dbg("%s - parity = none", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | if (cflag & CMSPAR) |
| 1087 | lParity = lParity | 0x20; |
| 1088 | |
| 1089 | /* Change the Stop bit */ |
| 1090 | if (cflag & CSTOPB) { |
| 1091 | lStop = UART_LCR_STOP; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1092 | dbg("%s - stop bits = 2", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1093 | } else { |
| 1094 | lStop = 0x00; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1095 | dbg("%s - stop bits = 1", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ |
| 1099 | #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ |
| 1100 | #define LCR_PAR_MASK 0x38 /* Mask for parity field */ |
| 1101 | |
| 1102 | /* Update the LCR with the correct value */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1103 | mos7720_port->shadowLCR &= |
| 1104 | ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1105 | mos7720_port->shadowLCR |= (lData | lParity | lStop); |
| 1106 | |
| 1107 | |
| 1108 | /* Disable Interrupts */ |
| 1109 | data = 0x00; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1110 | send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor, |
| 1111 | UART_IER, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1112 | |
| 1113 | data = 0x00; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1114 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1115 | |
| 1116 | data = 0xcf; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1117 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1118 | |
| 1119 | /* Send the updated LCR value to the mos7720 */ |
| 1120 | data = mos7720_port->shadowLCR; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1121 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1122 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1123 | data = 0x00b; |
| 1124 | mos7720_port->shadowMCR = data; |
| 1125 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
| 1126 | data = 0x00b; |
| 1127 | send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1128 | |
| 1129 | /* set up the MCR register and send it to the mos7720 */ |
| 1130 | mos7720_port->shadowMCR = UART_MCR_OUT2; |
| 1131 | if (cflag & CBAUD) |
| 1132 | mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS); |
| 1133 | |
| 1134 | if (cflag & CRTSCTS) { |
| 1135 | mos7720_port->shadowMCR |= (UART_MCR_XONANY); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1136 | /* To set hardware flow control to the specified * |
| 1137 | * serial port, in SP1/2_CONTROL_REG */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1138 | if (port->number) { |
| 1139 | data = 0x001; |
| 1140 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, |
| 1141 | 0x08, &data); |
| 1142 | } else { |
| 1143 | data = 0x002; |
| 1144 | send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, |
| 1145 | 0x08, &data); |
| 1146 | } |
| 1147 | } else { |
| 1148 | mos7720_port->shadowMCR &= ~(UART_MCR_XONANY); |
| 1149 | } |
| 1150 | |
| 1151 | data = mos7720_port->shadowMCR; |
| 1152 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data); |
| 1153 | |
| 1154 | /* Determine divisor based on baud rate */ |
| 1155 | baud = tty_get_baud_rate(tty); |
| 1156 | if (!baud) { |
| 1157 | /* pick a default, any default... */ |
| 1158 | dbg("Picked default baud..."); |
| 1159 | baud = 9600; |
| 1160 | } |
| 1161 | |
| 1162 | if (baud >= 230400) { |
| 1163 | set_higher_rates(mos7720_port, baud); |
| 1164 | /* Enable Interrupts */ |
| 1165 | data = 0x0c; |
| 1166 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data); |
| 1167 | return; |
| 1168 | } |
| 1169 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1170 | dbg("%s - baud rate = %d", __func__, baud); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1171 | status = send_cmd_write_baud_rate(mos7720_port, baud); |
Alan Cox | 65d063ab | 2008-01-03 17:01:18 +0000 | [diff] [blame] | 1172 | /* FIXME: needs to write actual resulting baud back not just |
| 1173 | blindly do so */ |
| 1174 | if (cflag & CBAUD) |
| 1175 | tty_encode_baud_rate(tty, baud, baud); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1176 | /* Enable Interrupts */ |
| 1177 | data = 0x0c; |
| 1178 | send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data); |
| 1179 | |
| 1180 | if (port->read_urb->status != -EINPROGRESS) { |
| 1181 | port->read_urb->dev = serial->dev; |
| 1182 | |
| 1183 | status = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 1184 | if (status) |
| 1185 | dbg("usb_submit_urb(read bulk) failed, status = %d", |
| 1186 | status); |
| 1187 | } |
| 1188 | return; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * mos7720_set_termios |
| 1193 | * this function is called by the tty driver when it wants to change the |
| 1194 | * termios structure. |
| 1195 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1196 | static void mos7720_set_termios(struct tty_struct *tty, |
| 1197 | struct usb_serial_port *port, struct ktermios *old_termios) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1198 | { |
| 1199 | int status; |
| 1200 | unsigned int cflag; |
| 1201 | struct usb_serial *serial; |
| 1202 | struct moschip_port *mos7720_port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1203 | |
| 1204 | serial = port->serial; |
| 1205 | |
| 1206 | mos7720_port = usb_get_serial_port_data(port); |
| 1207 | |
| 1208 | if (mos7720_port == NULL) |
| 1209 | return; |
| 1210 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1211 | if (!mos7720_port->open) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1212 | dbg("%s - port not opened", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1213 | return; |
| 1214 | } |
| 1215 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1216 | dbg("%s\n", "setting termios - ASPIRE"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1217 | |
| 1218 | cflag = tty->termios->c_cflag; |
| 1219 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1220 | dbg("%s - cflag %08x iflag %08x", __func__, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1221 | tty->termios->c_cflag, |
| 1222 | RELEVANT_IFLAG(tty->termios->c_iflag)); |
| 1223 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1224 | dbg("%s - old cflag %08x old iflag %08x", __func__, |
Alan Cox | 65d063ab | 2008-01-03 17:01:18 +0000 | [diff] [blame] | 1225 | old_termios->c_cflag, |
| 1226 | RELEVANT_IFLAG(old_termios->c_iflag)); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1227 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1228 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1229 | |
| 1230 | /* change the port settings to the new ones specified */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1231 | change_port_settings(tty, mos7720_port, old_termios); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1232 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1233 | if (!port->read_urb) { |
| 1234 | dbg("%s", "URB KILLED !!!!!\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1235 | return; |
| 1236 | } |
| 1237 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1238 | if (port->read_urb->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1239 | port->read_urb->dev = serial->dev; |
| 1240 | status = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 1241 | if (status) |
| 1242 | dbg("usb_submit_urb(read bulk) failed, status = %d", |
| 1243 | status); |
| 1244 | } |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * get_lsr_info - get line status register info |
| 1250 | * |
| 1251 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 1252 | * is emptied. On bus types like RS485, the transmitter must |
| 1253 | * release the bus after transmitting. This must be done when |
| 1254 | * the transmit shift register is empty, not be done when the |
| 1255 | * transmit holding register is empty. This functionality |
| 1256 | * allows an RS485 driver to be written in user space. |
| 1257 | */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1258 | static int get_lsr_info(struct tty_struct *tty, |
| 1259 | struct moschip_port *mos7720_port, unsigned int __user *value) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1260 | { |
| 1261 | int count; |
| 1262 | unsigned int result = 0; |
| 1263 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1264 | count = mos7720_chars_in_buffer(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1265 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1266 | dbg("%s -- Empty", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1267 | result = TIOCSER_TEMT; |
| 1268 | } |
| 1269 | |
| 1270 | if (copy_to_user(value, &result, sizeof(int))) |
| 1271 | return -EFAULT; |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1275 | static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd, |
| 1276 | unsigned int __user *value) |
| 1277 | { |
| 1278 | unsigned int mcr ; |
| 1279 | unsigned int arg; |
| 1280 | unsigned char data; |
| 1281 | |
| 1282 | struct usb_serial_port *port; |
| 1283 | |
| 1284 | if (mos7720_port == NULL) |
| 1285 | return -1; |
| 1286 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1287 | port = (struct usb_serial_port *)mos7720_port->port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1288 | mcr = mos7720_port->shadowMCR; |
| 1289 | |
| 1290 | if (copy_from_user(&arg, value, sizeof(int))) |
| 1291 | return -EFAULT; |
| 1292 | |
| 1293 | switch (cmd) { |
| 1294 | case TIOCMBIS: |
| 1295 | if (arg & TIOCM_RTS) |
| 1296 | mcr |= UART_MCR_RTS; |
| 1297 | if (arg & TIOCM_DTR) |
| 1298 | mcr |= UART_MCR_RTS; |
| 1299 | if (arg & TIOCM_LOOP) |
| 1300 | mcr |= UART_MCR_LOOP; |
| 1301 | break; |
| 1302 | |
| 1303 | case TIOCMBIC: |
| 1304 | if (arg & TIOCM_RTS) |
| 1305 | mcr &= ~UART_MCR_RTS; |
| 1306 | if (arg & TIOCM_DTR) |
| 1307 | mcr &= ~UART_MCR_RTS; |
| 1308 | if (arg & TIOCM_LOOP) |
| 1309 | mcr &= ~UART_MCR_LOOP; |
| 1310 | break; |
| 1311 | |
| 1312 | case TIOCMSET: |
| 1313 | /* turn off the RTS and DTR and LOOPBACK |
| 1314 | * and then only turn on what was asked to */ |
| 1315 | mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP); |
| 1316 | mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0); |
| 1317 | mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0); |
| 1318 | mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0); |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | mos7720_port->shadowMCR = mcr; |
| 1323 | |
| 1324 | data = mos7720_port->shadowMCR; |
| 1325 | send_mos_cmd(port->serial, MOS_WRITE, |
| 1326 | port->number - port->serial->minor, UART_MCR, &data); |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static int get_modem_info(struct moschip_port *mos7720_port, |
| 1332 | unsigned int __user *value) |
| 1333 | { |
| 1334 | unsigned int result = 0; |
| 1335 | unsigned int msr = mos7720_port->shadowMSR; |
| 1336 | unsigned int mcr = mos7720_port->shadowMCR; |
| 1337 | |
| 1338 | result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */ |
| 1339 | | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */ |
| 1340 | | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */ |
| 1341 | | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */ |
| 1342 | | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */ |
| 1343 | | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */ |
| 1344 | |
| 1345 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1346 | dbg("%s -- %x", __func__, result); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (copy_to_user(value, &result, sizeof(int))) |
| 1349 | return -EFAULT; |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | static int get_serial_info(struct moschip_port *mos7720_port, |
| 1354 | struct serial_struct __user *retinfo) |
| 1355 | { |
| 1356 | struct serial_struct tmp; |
| 1357 | |
| 1358 | if (!retinfo) |
| 1359 | return -EFAULT; |
| 1360 | |
| 1361 | memset(&tmp, 0, sizeof(tmp)); |
| 1362 | |
| 1363 | tmp.type = PORT_16550A; |
| 1364 | tmp.line = mos7720_port->port->serial->minor; |
| 1365 | tmp.port = mos7720_port->port->number; |
| 1366 | tmp.irq = 0; |
| 1367 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1368 | tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1369 | tmp.baud_base = 9600; |
| 1370 | tmp.close_delay = 5*HZ; |
| 1371 | tmp.closing_wait = 30*HZ; |
| 1372 | |
| 1373 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 1374 | return -EFAULT; |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1378 | static int mos7720_ioctl(struct tty_struct *tty, struct file *file, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1379 | unsigned int cmd, unsigned long arg) |
| 1380 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1381 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1382 | struct moschip_port *mos7720_port; |
| 1383 | struct async_icount cnow; |
| 1384 | struct async_icount cprev; |
| 1385 | struct serial_icounter_struct icount; |
| 1386 | |
| 1387 | mos7720_port = usb_get_serial_port_data(port); |
| 1388 | if (mos7720_port == NULL) |
| 1389 | return -ENODEV; |
| 1390 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1391 | dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1392 | |
| 1393 | switch (cmd) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1394 | case TIOCSERGETLSR: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1395 | dbg("%s (%d) TIOCSERGETLSR", __func__, port->number); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1396 | return get_lsr_info(tty, mos7720_port, |
| 1397 | (unsigned int __user *)arg); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1398 | return 0; |
| 1399 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1400 | /* FIXME: These should be using the mode methods */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1401 | case TIOCMBIS: |
| 1402 | case TIOCMBIC: |
| 1403 | case TIOCMSET: |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1404 | dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", |
| 1405 | __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1406 | return set_modem_info(mos7720_port, cmd, |
| 1407 | (unsigned int __user *)arg); |
| 1408 | |
| 1409 | case TIOCMGET: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1410 | dbg("%s (%d) TIOCMGET", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1411 | return get_modem_info(mos7720_port, |
| 1412 | (unsigned int __user *)arg); |
| 1413 | |
| 1414 | case TIOCGSERIAL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1415 | dbg("%s (%d) TIOCGSERIAL", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1416 | return get_serial_info(mos7720_port, |
| 1417 | (struct serial_struct __user *)arg); |
| 1418 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1419 | case TIOCMIWAIT: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1420 | dbg("%s (%d) TIOCMIWAIT", __func__, port->number); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1421 | cprev = mos7720_port->icount; |
| 1422 | while (1) { |
| 1423 | if (signal_pending(current)) |
| 1424 | return -ERESTARTSYS; |
| 1425 | cnow = mos7720_port->icount; |
| 1426 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && |
| 1427 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) |
| 1428 | return -EIO; /* no change => error */ |
| 1429 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 1430 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 1431 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1432 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | cprev = cnow; |
| 1436 | } |
| 1437 | /* NOTREACHED */ |
| 1438 | break; |
| 1439 | |
| 1440 | case TIOCGICOUNT: |
| 1441 | cnow = mos7720_port->icount; |
| 1442 | icount.cts = cnow.cts; |
| 1443 | icount.dsr = cnow.dsr; |
| 1444 | icount.rng = cnow.rng; |
| 1445 | icount.dcd = cnow.dcd; |
| 1446 | icount.rx = cnow.rx; |
| 1447 | icount.tx = cnow.tx; |
| 1448 | icount.frame = cnow.frame; |
| 1449 | icount.overrun = cnow.overrun; |
| 1450 | icount.parity = cnow.parity; |
| 1451 | icount.brk = cnow.brk; |
| 1452 | icount.buf_overrun = cnow.buf_overrun; |
| 1453 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1454 | dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1455 | port->number, icount.rx, icount.tx); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1456 | if (copy_to_user((void __user *)arg, &icount, sizeof(icount))) |
| 1457 | return -EFAULT; |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | return -ENOIOCTLCMD; |
| 1462 | } |
| 1463 | |
| 1464 | static int mos7720_startup(struct usb_serial *serial) |
| 1465 | { |
| 1466 | struct moschip_serial *mos7720_serial; |
| 1467 | struct moschip_port *mos7720_port; |
| 1468 | struct usb_device *dev; |
| 1469 | int i; |
| 1470 | char data; |
| 1471 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1472 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1473 | |
| 1474 | if (!serial) { |
| 1475 | dbg("Invalid Handler"); |
| 1476 | return -ENODEV; |
| 1477 | } |
| 1478 | |
| 1479 | dev = serial->dev; |
| 1480 | |
| 1481 | /* create our private serial structure */ |
| 1482 | mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL); |
| 1483 | if (mos7720_serial == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1484 | dev_err(&dev->dev, "%s - Out of memory\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1485 | return -ENOMEM; |
| 1486 | } |
| 1487 | |
| 1488 | usb_set_serial_data(serial, mos7720_serial); |
| 1489 | |
| 1490 | /* we set up the pointers to the endpoints in the mos7720_open * |
| 1491 | * function, as the structures aren't created yet. */ |
| 1492 | |
| 1493 | /* set up port private structures */ |
| 1494 | for (i = 0; i < serial->num_ports; ++i) { |
| 1495 | mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); |
| 1496 | if (mos7720_port == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1497 | dev_err(&dev->dev, "%s - Out of memory\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1498 | usb_set_serial_data(serial, NULL); |
| 1499 | kfree(mos7720_serial); |
| 1500 | return -ENOMEM; |
| 1501 | } |
| 1502 | |
| 1503 | /* Initialize all port interrupt end point to port 0 int |
| 1504 | * endpoint. Our device has only one interrupt endpoint |
| 1505 | * comman to all ports */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1506 | serial->port[i]->interrupt_in_endpointAddress = |
| 1507 | serial->port[0]->interrupt_in_endpointAddress; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1508 | |
| 1509 | mos7720_port->port = serial->port[i]; |
| 1510 | usb_set_serial_port_data(serial->port[i], mos7720_port); |
| 1511 | |
| 1512 | dbg("port number is %d", serial->port[i]->number); |
| 1513 | dbg("serial number is %d", serial->minor); |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | /* setting configuration feature to one */ |
| 1518 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1519 | (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1520 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1521 | /* LSR For Port 1 */ |
| 1522 | send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data); |
| 1523 | dbg("LSR:%x", data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1524 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1525 | /* LSR For Port 2 */ |
| 1526 | send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data); |
| 1527 | dbg("LSR:%x", data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1528 | |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | static void mos7720_shutdown(struct usb_serial *serial) |
| 1533 | { |
| 1534 | int i; |
| 1535 | |
| 1536 | /* free private structure allocated for serial port */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1537 | for (i = 0; i < serial->num_ports; ++i) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1538 | kfree(usb_get_serial_port_data(serial->port[i])); |
| 1539 | usb_set_serial_port_data(serial->port[i], NULL); |
| 1540 | } |
| 1541 | |
| 1542 | /* free private structure allocated for serial device */ |
| 1543 | kfree(usb_get_serial_data(serial)); |
| 1544 | usb_set_serial_data(serial, NULL); |
| 1545 | } |
| 1546 | |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 1547 | static struct usb_driver usb_driver = { |
| 1548 | .name = "moschip7720", |
| 1549 | .probe = usb_serial_probe, |
| 1550 | .disconnect = usb_serial_disconnect, |
| 1551 | .id_table = moschip_port_id_table, |
| 1552 | .no_dynamic_id = 1, |
| 1553 | }; |
| 1554 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1555 | static struct usb_serial_driver moschip7720_2port_driver = { |
| 1556 | .driver = { |
| 1557 | .owner = THIS_MODULE, |
| 1558 | .name = "moschip7720", |
| 1559 | }, |
| 1560 | .description = "Moschip 2 port adapter", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 1561 | .usb_driver = &usb_driver, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1562 | .id_table = moschip_port_id_table, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1563 | .num_ports = 2, |
| 1564 | .open = mos7720_open, |
| 1565 | .close = mos7720_close, |
| 1566 | .throttle = mos7720_throttle, |
| 1567 | .unthrottle = mos7720_unthrottle, |
| 1568 | .attach = mos7720_startup, |
| 1569 | .shutdown = mos7720_shutdown, |
| 1570 | .ioctl = mos7720_ioctl, |
| 1571 | .set_termios = mos7720_set_termios, |
| 1572 | .write = mos7720_write, |
| 1573 | .write_room = mos7720_write_room, |
| 1574 | .chars_in_buffer = mos7720_chars_in_buffer, |
| 1575 | .break_ctl = mos7720_break, |
| 1576 | .read_bulk_callback = mos7720_bulk_in_callback, |
Oliver Neukum | e8e30c7 | 2007-03-14 11:11:08 +0100 | [diff] [blame] | 1577 | .read_int_callback = mos7720_interrupt_callback, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1578 | }; |
| 1579 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1580 | static int __init moschip7720_init(void) |
| 1581 | { |
| 1582 | int retval; |
| 1583 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1584 | dbg("%s: Entering ..........", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* Register with the usb serial */ |
| 1587 | retval = usb_serial_register(&moschip7720_2port_driver); |
| 1588 | if (retval) |
| 1589 | goto failed_port_device_register; |
| 1590 | |
Greg Kroah-Hartman | c197a8d | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 1591 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 1592 | DRIVER_DESC "\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1593 | |
| 1594 | /* Register with the usb */ |
| 1595 | retval = usb_register(&usb_driver); |
| 1596 | if (retval) |
| 1597 | goto failed_usb_register; |
| 1598 | |
| 1599 | return 0; |
| 1600 | |
| 1601 | failed_usb_register: |
| 1602 | usb_serial_deregister(&moschip7720_2port_driver); |
| 1603 | |
| 1604 | failed_port_device_register: |
| 1605 | return retval; |
| 1606 | } |
| 1607 | |
| 1608 | static void __exit moschip7720_exit(void) |
| 1609 | { |
| 1610 | usb_deregister(&usb_driver); |
| 1611 | usb_serial_deregister(&moschip7720_2port_driver); |
| 1612 | } |
| 1613 | |
| 1614 | module_init(moschip7720_init); |
| 1615 | module_exit(moschip7720_exit); |
| 1616 | |
| 1617 | /* Module information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1618 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1619 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1620 | MODULE_LICENSE("GPL"); |
| 1621 | |
| 1622 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1623 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |