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> |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 37 | #include <linux/parport.h> |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Version Information |
| 41 | */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 42 | #define DRIVER_VERSION "2.1" |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 49 | #define MOS_MAX_PORT 0x02 |
| 50 | #define MOS_WRITE 0x0E |
| 51 | #define MOS_READ 0x0D |
| 52 | |
| 53 | /* Interrupt Rotinue Defines */ |
| 54 | #define SERIAL_IIR_RLS 0x06 |
| 55 | #define SERIAL_IIR_RDA 0x04 |
| 56 | #define SERIAL_IIR_CTI 0x0c |
| 57 | #define SERIAL_IIR_THR 0x02 |
| 58 | #define SERIAL_IIR_MS 0x00 |
| 59 | |
| 60 | #define NUM_URBS 16 /* URB Count */ |
| 61 | #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ |
| 62 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 63 | /* This structure holds all of the local serial port information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 64 | struct moschip_port { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 65 | __u8 shadowLCR; /* last LCR value received */ |
| 66 | __u8 shadowMCR; /* last MCR value received */ |
| 67 | __u8 shadowMSR; /* last MSR value received */ |
| 68 | char open; |
| 69 | struct async_icount icount; |
| 70 | struct usb_serial_port *port; /* loop back to the owner */ |
| 71 | struct urb *write_urb_pool[NUM_URBS]; |
| 72 | }; |
| 73 | |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 74 | static struct usb_serial_driver moschip7720_2port_driver; |
| 75 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 76 | #define USB_VENDOR_ID_MOSCHIP 0x9710 |
| 77 | #define MOSCHIP_DEVICE_ID_7720 0x7720 |
| 78 | #define MOSCHIP_DEVICE_ID_7715 0x7715 |
| 79 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 80 | static const struct usb_device_id id_table[] = { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 81 | { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) }, |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 82 | { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) }, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 83 | { } /* terminating entry */ |
| 84 | }; |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 85 | MODULE_DEVICE_TABLE(usb, id_table); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 86 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 87 | #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT |
| 88 | |
| 89 | /* initial values for parport regs */ |
| 90 | #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */ |
| 91 | #define ECR_INIT_VAL 0x00 /* SPP mode */ |
| 92 | |
| 93 | struct urbtracker { |
| 94 | struct mos7715_parport *mos_parport; |
| 95 | struct list_head urblist_entry; |
| 96 | struct kref ref_count; |
| 97 | struct urb *urb; |
| 98 | }; |
| 99 | |
| 100 | enum mos7715_pp_modes { |
| 101 | SPP = 0<<5, |
| 102 | PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */ |
| 103 | PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */ |
| 104 | }; |
| 105 | |
| 106 | struct mos7715_parport { |
| 107 | struct parport *pp; /* back to containing struct */ |
| 108 | struct kref ref_count; /* to instance of this struct */ |
| 109 | struct list_head deferred_urbs; /* list deferred async urbs */ |
| 110 | struct list_head active_urbs; /* list async urbs in flight */ |
| 111 | spinlock_t listlock; /* protects list access */ |
| 112 | bool msg_pending; /* usb sync call pending */ |
| 113 | struct completion syncmsg_compl; /* usb sync call completed */ |
| 114 | struct tasklet_struct urb_tasklet; /* for sending deferred urbs */ |
| 115 | struct usb_serial *serial; /* back to containing struct */ |
| 116 | __u8 shadowECR; /* parallel port regs... */ |
| 117 | __u8 shadowDCR; |
| 118 | atomic_t shadowDSR; /* updated in int-in callback */ |
| 119 | }; |
| 120 | |
| 121 | /* lock guards against dereferencing NULL ptr in parport ops callbacks */ |
| 122 | static DEFINE_SPINLOCK(release_lock); |
| 123 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 124 | #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ |
| 125 | |
| 126 | static const unsigned int dummy; /* for clarity in register access fns */ |
| 127 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 128 | enum mos_regs { |
| 129 | THR, /* serial port regs */ |
| 130 | RHR, |
| 131 | IER, |
| 132 | FCR, |
| 133 | ISR, |
| 134 | LCR, |
| 135 | MCR, |
| 136 | LSR, |
| 137 | MSR, |
| 138 | SPR, |
| 139 | DLL, |
| 140 | DLM, |
| 141 | DPR, /* parallel port regs */ |
| 142 | DSR, |
| 143 | DCR, |
| 144 | ECR, |
| 145 | SP1_REG, /* device control regs */ |
| 146 | SP2_REG, /* serial port 2 (7720 only) */ |
| 147 | PP_REG, |
| 148 | SP_CONTROL_REG, |
| 149 | }; |
| 150 | |
| 151 | /* |
| 152 | * Return the correct value for the Windex field of the setup packet |
| 153 | * for a control endpoint message. See the 7715 datasheet. |
| 154 | */ |
| 155 | static inline __u16 get_reg_index(enum mos_regs reg) |
| 156 | { |
| 157 | static const __u16 mos7715_index_lookup_table[] = { |
| 158 | 0x00, /* THR */ |
| 159 | 0x00, /* RHR */ |
| 160 | 0x01, /* IER */ |
| 161 | 0x02, /* FCR */ |
| 162 | 0x02, /* ISR */ |
| 163 | 0x03, /* LCR */ |
| 164 | 0x04, /* MCR */ |
| 165 | 0x05, /* LSR */ |
| 166 | 0x06, /* MSR */ |
| 167 | 0x07, /* SPR */ |
| 168 | 0x00, /* DLL */ |
| 169 | 0x01, /* DLM */ |
| 170 | 0x00, /* DPR */ |
| 171 | 0x01, /* DSR */ |
| 172 | 0x02, /* DCR */ |
| 173 | 0x0a, /* ECR */ |
| 174 | 0x01, /* SP1_REG */ |
| 175 | 0x02, /* SP2_REG (7720 only) */ |
| 176 | 0x04, /* PP_REG (7715 only) */ |
| 177 | 0x08, /* SP_CONTROL_REG */ |
| 178 | }; |
| 179 | return mos7715_index_lookup_table[reg]; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Return the correct value for the upper byte of the Wvalue field of |
| 184 | * the setup packet for a control endpoint message. |
| 185 | */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 186 | static inline __u16 get_reg_value(enum mos_regs reg, |
| 187 | unsigned int serial_portnum) |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 188 | { |
| 189 | if (reg >= SP1_REG) /* control reg */ |
| 190 | return 0x0000; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 191 | |
| 192 | else if (reg >= DPR) /* parallel port reg (7715 only) */ |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 193 | return 0x0100; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 194 | |
| 195 | else /* serial port reg */ |
| 196 | return (serial_portnum + 2) << 8; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Write data byte to the specified device register. The data is embedded in |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 201 | * the value field of the setup packet. serial_portnum is ignored for registers |
| 202 | * not specific to a particular serial port. |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 203 | */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 204 | static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, |
| 205 | enum mos_regs reg, __u8 data) |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 206 | { |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 207 | struct usb_device *usbdev = serial->dev; |
| 208 | unsigned int pipe = usb_sndctrlpipe(usbdev, 0); |
| 209 | __u8 request = (__u8)0x0e; |
| 210 | __u8 requesttype = (__u8)0x40; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 211 | __u16 index = get_reg_index(reg); |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 212 | __u16 value = get_reg_value(reg, serial_portnum) + data; |
| 213 | int status = usb_control_msg(usbdev, pipe, request, requesttype, value, |
| 214 | index, NULL, 0, MOS_WDR_TIMEOUT); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 215 | if (status < 0) |
| 216 | dev_err(&usbdev->dev, |
| 217 | "mos7720: usb_control_msg() failed: %d", status); |
| 218 | return status; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Read data byte from the specified device register. The data returned by the |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 223 | * device is embedded in the value field of the setup packet. serial_portnum is |
| 224 | * ignored for registers that are not specific to a particular serial port. |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 225 | */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 226 | static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, |
| 227 | enum mos_regs reg, __u8 *data) |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 228 | { |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 229 | struct usb_device *usbdev = serial->dev; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 230 | unsigned int pipe = usb_rcvctrlpipe(usbdev, 0); |
| 231 | __u8 request = (__u8)0x0d; |
| 232 | __u8 requesttype = (__u8)0xc0; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 233 | __u16 index = get_reg_index(reg); |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 234 | __u16 value = get_reg_value(reg, serial_portnum); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 235 | int status = usb_control_msg(usbdev, pipe, request, requesttype, value, |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 236 | index, data, 1, MOS_WDR_TIMEOUT); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 237 | if (status < 0) |
| 238 | dev_err(&usbdev->dev, |
| 239 | "mos7720: usb_control_msg() failed: %d", status); |
| 240 | return status; |
| 241 | } |
| 242 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 243 | #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT |
| 244 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 245 | static inline int mos7715_change_mode(struct mos7715_parport *mos_parport, |
| 246 | enum mos7715_pp_modes mode) |
| 247 | { |
| 248 | mos_parport->shadowECR = mode; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 249 | write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static void destroy_mos_parport(struct kref *kref) |
| 254 | { |
| 255 | struct mos7715_parport *mos_parport = |
| 256 | container_of(kref, struct mos7715_parport, ref_count); |
| 257 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 258 | kfree(mos_parport); |
| 259 | } |
| 260 | |
| 261 | static void destroy_urbtracker(struct kref *kref) |
| 262 | { |
| 263 | struct urbtracker *urbtrack = |
| 264 | container_of(kref, struct urbtracker, ref_count); |
| 265 | struct mos7715_parport *mos_parport = urbtrack->mos_parport; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 266 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 267 | usb_free_urb(urbtrack->urb); |
| 268 | kfree(urbtrack); |
| 269 | kref_put(&mos_parport->ref_count, destroy_mos_parport); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * This runs as a tasklet when sending an urb in a non-blocking parallel |
| 274 | * port callback had to be deferred because the disconnect mutex could not be |
| 275 | * obtained at the time. |
| 276 | */ |
| 277 | static void send_deferred_urbs(unsigned long _mos_parport) |
| 278 | { |
| 279 | int ret_val; |
| 280 | unsigned long flags; |
| 281 | struct mos7715_parport *mos_parport = (void *)_mos_parport; |
Wei Yongjun | 6799047 | 2012-08-21 11:28:45 +0800 | [diff] [blame] | 282 | struct urbtracker *urbtrack, *tmp; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 283 | struct list_head *cursor, *next; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 284 | struct device *dev; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 285 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 286 | /* if release function ran, game over */ |
| 287 | if (unlikely(mos_parport->serial == NULL)) |
| 288 | return; |
| 289 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 290 | dev = &mos_parport->serial->dev->dev; |
| 291 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 292 | /* try again to get the mutex */ |
| 293 | if (!mutex_trylock(&mos_parport->serial->disc_mutex)) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 294 | dev_dbg(dev, "%s: rescheduling tasklet\n", __func__); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 295 | tasklet_schedule(&mos_parport->urb_tasklet); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | /* if device disconnected, game over */ |
| 300 | if (unlikely(mos_parport->serial->disconnected)) { |
| 301 | mutex_unlock(&mos_parport->serial->disc_mutex); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | spin_lock_irqsave(&mos_parport->listlock, flags); |
| 306 | if (list_empty(&mos_parport->deferred_urbs)) { |
| 307 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 308 | mutex_unlock(&mos_parport->serial->disc_mutex); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 309 | dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
| 313 | /* move contents of deferred_urbs list to active_urbs list and submit */ |
| 314 | list_for_each_safe(cursor, next, &mos_parport->deferred_urbs) |
| 315 | list_move_tail(cursor, &mos_parport->active_urbs); |
Wei Yongjun | 6799047 | 2012-08-21 11:28:45 +0800 | [diff] [blame] | 316 | list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs, |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 317 | urblist_entry) { |
| 318 | ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 319 | dev_dbg(dev, "%s: urb submitted\n", __func__); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 320 | if (ret_val) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 321 | dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 322 | list_del(&urbtrack->urblist_entry); |
| 323 | kref_put(&urbtrack->ref_count, destroy_urbtracker); |
| 324 | } |
| 325 | } |
| 326 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 327 | mutex_unlock(&mos_parport->serial->disc_mutex); |
| 328 | } |
| 329 | |
| 330 | /* callback for parallel port control urbs submitted asynchronously */ |
| 331 | static void async_complete(struct urb *urb) |
| 332 | { |
| 333 | struct urbtracker *urbtrack = urb->context; |
| 334 | int status = urb->status; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 335 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 336 | if (unlikely(status)) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 337 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 338 | |
| 339 | /* remove the urbtracker from the active_urbs list */ |
| 340 | spin_lock(&urbtrack->mos_parport->listlock); |
| 341 | list_del(&urbtrack->urblist_entry); |
| 342 | spin_unlock(&urbtrack->mos_parport->listlock); |
| 343 | kref_put(&urbtrack->ref_count, destroy_urbtracker); |
| 344 | } |
| 345 | |
| 346 | static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport, |
| 347 | enum mos_regs reg, __u8 data) |
| 348 | { |
| 349 | struct urbtracker *urbtrack; |
| 350 | int ret_val; |
| 351 | unsigned long flags; |
| 352 | struct usb_ctrlrequest setup; |
| 353 | struct usb_serial *serial = mos_parport->serial; |
| 354 | struct usb_device *usbdev = serial->dev; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 355 | |
| 356 | /* create and initialize the control urb and containing urbtracker */ |
| 357 | urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC); |
| 358 | if (urbtrack == NULL) { |
| 359 | dev_err(&usbdev->dev, "out of memory"); |
| 360 | return -ENOMEM; |
| 361 | } |
| 362 | kref_get(&mos_parport->ref_count); |
| 363 | urbtrack->mos_parport = mos_parport; |
| 364 | urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 365 | if (urbtrack->urb == NULL) { |
| 366 | dev_err(&usbdev->dev, "out of urbs"); |
| 367 | kfree(urbtrack); |
| 368 | return -ENOMEM; |
| 369 | } |
| 370 | setup.bRequestType = (__u8)0x40; |
| 371 | setup.bRequest = (__u8)0x0e; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 372 | setup.wValue = get_reg_value(reg, dummy); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 373 | setup.wIndex = get_reg_index(reg); |
| 374 | setup.wLength = 0; |
| 375 | usb_fill_control_urb(urbtrack->urb, usbdev, |
| 376 | usb_sndctrlpipe(usbdev, 0), |
| 377 | (unsigned char *)&setup, |
| 378 | NULL, 0, async_complete, urbtrack); |
| 379 | kref_init(&urbtrack->ref_count); |
| 380 | INIT_LIST_HEAD(&urbtrack->urblist_entry); |
| 381 | |
| 382 | /* |
| 383 | * get the disconnect mutex, or add tracker to the deferred_urbs list |
| 384 | * and schedule a tasklet to try again later |
| 385 | */ |
| 386 | if (!mutex_trylock(&serial->disc_mutex)) { |
| 387 | spin_lock_irqsave(&mos_parport->listlock, flags); |
| 388 | list_add_tail(&urbtrack->urblist_entry, |
| 389 | &mos_parport->deferred_urbs); |
| 390 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 391 | tasklet_schedule(&mos_parport->urb_tasklet); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 392 | dev_dbg(&usbdev->dev, "tasklet scheduled"); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /* bail if device disconnected */ |
| 397 | if (serial->disconnected) { |
| 398 | kref_put(&urbtrack->ref_count, destroy_urbtracker); |
| 399 | mutex_unlock(&serial->disc_mutex); |
| 400 | return -ENODEV; |
| 401 | } |
| 402 | |
| 403 | /* add the tracker to the active_urbs list and submit */ |
| 404 | spin_lock_irqsave(&mos_parport->listlock, flags); |
| 405 | list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs); |
| 406 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 407 | ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC); |
| 408 | mutex_unlock(&serial->disc_mutex); |
| 409 | if (ret_val) { |
| 410 | dev_err(&usbdev->dev, |
| 411 | "%s: submit_urb() failed: %d", __func__, ret_val); |
| 412 | spin_lock_irqsave(&mos_parport->listlock, flags); |
| 413 | list_del(&urbtrack->urblist_entry); |
| 414 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 415 | kref_put(&urbtrack->ref_count, destroy_urbtracker); |
| 416 | return ret_val; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * This is the the common top part of all parallel port callback operations that |
| 423 | * send synchronous messages to the device. This implements convoluted locking |
| 424 | * that avoids two scenarios: (1) a port operation is called after usbserial |
| 425 | * has called our release function, at which point struct mos7715_parport has |
| 426 | * been destroyed, and (2) the device has been disconnected, but usbserial has |
| 427 | * not called the release function yet because someone has a serial port open. |
| 428 | * The shared release_lock prevents the first, and the mutex and disconnected |
| 429 | * flag maintained by usbserial covers the second. We also use the msg_pending |
| 430 | * flag to ensure that all synchronous usb messgage calls have completed before |
| 431 | * our release function can return. |
| 432 | */ |
| 433 | static int parport_prologue(struct parport *pp) |
| 434 | { |
| 435 | struct mos7715_parport *mos_parport; |
| 436 | |
| 437 | spin_lock(&release_lock); |
| 438 | mos_parport = pp->private_data; |
| 439 | if (unlikely(mos_parport == NULL)) { |
| 440 | /* release fn called, port struct destroyed */ |
| 441 | spin_unlock(&release_lock); |
| 442 | return -1; |
| 443 | } |
| 444 | mos_parport->msg_pending = true; /* synch usb call pending */ |
| 445 | INIT_COMPLETION(mos_parport->syncmsg_compl); |
| 446 | spin_unlock(&release_lock); |
| 447 | |
| 448 | mutex_lock(&mos_parport->serial->disc_mutex); |
| 449 | if (mos_parport->serial->disconnected) { |
| 450 | /* device disconnected */ |
| 451 | mutex_unlock(&mos_parport->serial->disc_mutex); |
| 452 | mos_parport->msg_pending = false; |
| 453 | complete(&mos_parport->syncmsg_compl); |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * This is the the common bottom part of all parallel port functions that send |
| 462 | * synchronous messages to the device. |
| 463 | */ |
| 464 | static inline void parport_epilogue(struct parport *pp) |
| 465 | { |
| 466 | struct mos7715_parport *mos_parport = pp->private_data; |
| 467 | mutex_unlock(&mos_parport->serial->disc_mutex); |
| 468 | mos_parport->msg_pending = false; |
| 469 | complete(&mos_parport->syncmsg_compl); |
| 470 | } |
| 471 | |
| 472 | static void parport_mos7715_write_data(struct parport *pp, unsigned char d) |
| 473 | { |
| 474 | struct mos7715_parport *mos_parport = pp->private_data; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 475 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 476 | if (parport_prologue(pp) < 0) |
| 477 | return; |
| 478 | mos7715_change_mode(mos_parport, SPP); |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 479 | write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 480 | parport_epilogue(pp); |
| 481 | } |
| 482 | |
| 483 | static unsigned char parport_mos7715_read_data(struct parport *pp) |
| 484 | { |
| 485 | struct mos7715_parport *mos_parport = pp->private_data; |
| 486 | unsigned char d; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 487 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 488 | if (parport_prologue(pp) < 0) |
| 489 | return 0; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 490 | read_mos_reg(mos_parport->serial, dummy, DPR, &d); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 491 | parport_epilogue(pp); |
| 492 | return d; |
| 493 | } |
| 494 | |
| 495 | static void parport_mos7715_write_control(struct parport *pp, unsigned char d) |
| 496 | { |
| 497 | struct mos7715_parport *mos_parport = pp->private_data; |
| 498 | __u8 data; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 499 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 500 | if (parport_prologue(pp) < 0) |
| 501 | return; |
| 502 | data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0); |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 503 | write_mos_reg(mos_parport->serial, dummy, DCR, data); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 504 | mos_parport->shadowDCR = data; |
| 505 | parport_epilogue(pp); |
| 506 | } |
| 507 | |
| 508 | static unsigned char parport_mos7715_read_control(struct parport *pp) |
| 509 | { |
| 510 | struct mos7715_parport *mos_parport = pp->private_data; |
| 511 | __u8 dcr; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 512 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 513 | spin_lock(&release_lock); |
| 514 | mos_parport = pp->private_data; |
| 515 | if (unlikely(mos_parport == NULL)) { |
| 516 | spin_unlock(&release_lock); |
| 517 | return 0; |
| 518 | } |
| 519 | dcr = mos_parport->shadowDCR & 0x0f; |
| 520 | spin_unlock(&release_lock); |
| 521 | return dcr; |
| 522 | } |
| 523 | |
| 524 | static unsigned char parport_mos7715_frob_control(struct parport *pp, |
| 525 | unsigned char mask, |
| 526 | unsigned char val) |
| 527 | { |
| 528 | struct mos7715_parport *mos_parport = pp->private_data; |
| 529 | __u8 dcr; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 530 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 531 | mask &= 0x0f; |
| 532 | val &= 0x0f; |
| 533 | if (parport_prologue(pp) < 0) |
| 534 | return 0; |
| 535 | mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 536 | write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 537 | dcr = mos_parport->shadowDCR & 0x0f; |
| 538 | parport_epilogue(pp); |
| 539 | return dcr; |
| 540 | } |
| 541 | |
| 542 | static unsigned char parport_mos7715_read_status(struct parport *pp) |
| 543 | { |
| 544 | unsigned char status; |
| 545 | struct mos7715_parport *mos_parport = pp->private_data; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 546 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 547 | spin_lock(&release_lock); |
| 548 | mos_parport = pp->private_data; |
| 549 | if (unlikely(mos_parport == NULL)) { /* release called */ |
| 550 | spin_unlock(&release_lock); |
| 551 | return 0; |
| 552 | } |
| 553 | status = atomic_read(&mos_parport->shadowDSR) & 0xf8; |
| 554 | spin_unlock(&release_lock); |
| 555 | return status; |
| 556 | } |
| 557 | |
| 558 | static void parport_mos7715_enable_irq(struct parport *pp) |
| 559 | { |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 560 | } |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 561 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 562 | static void parport_mos7715_disable_irq(struct parport *pp) |
| 563 | { |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static void parport_mos7715_data_forward(struct parport *pp) |
| 567 | { |
| 568 | struct mos7715_parport *mos_parport = pp->private_data; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 569 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 570 | if (parport_prologue(pp) < 0) |
| 571 | return; |
| 572 | mos7715_change_mode(mos_parport, PS2); |
| 573 | mos_parport->shadowDCR &= ~0x20; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 574 | write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 575 | parport_epilogue(pp); |
| 576 | } |
| 577 | |
| 578 | static void parport_mos7715_data_reverse(struct parport *pp) |
| 579 | { |
| 580 | struct mos7715_parport *mos_parport = pp->private_data; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 581 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 582 | if (parport_prologue(pp) < 0) |
| 583 | return; |
| 584 | mos7715_change_mode(mos_parport, PS2); |
| 585 | mos_parport->shadowDCR |= 0x20; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 586 | write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 587 | parport_epilogue(pp); |
| 588 | } |
| 589 | |
| 590 | static void parport_mos7715_init_state(struct pardevice *dev, |
| 591 | struct parport_state *s) |
| 592 | { |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 593 | s->u.pc.ctr = DCR_INIT_VAL; |
| 594 | s->u.pc.ecr = ECR_INIT_VAL; |
| 595 | } |
| 596 | |
| 597 | /* N.B. Parport core code requires that this function not block */ |
| 598 | static void parport_mos7715_save_state(struct parport *pp, |
| 599 | struct parport_state *s) |
| 600 | { |
| 601 | struct mos7715_parport *mos_parport; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 602 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 603 | spin_lock(&release_lock); |
| 604 | mos_parport = pp->private_data; |
| 605 | if (unlikely(mos_parport == NULL)) { /* release called */ |
| 606 | spin_unlock(&release_lock); |
| 607 | return; |
| 608 | } |
| 609 | s->u.pc.ctr = mos_parport->shadowDCR; |
| 610 | s->u.pc.ecr = mos_parport->shadowECR; |
| 611 | spin_unlock(&release_lock); |
| 612 | } |
| 613 | |
| 614 | /* N.B. Parport core code requires that this function not block */ |
| 615 | static void parport_mos7715_restore_state(struct parport *pp, |
| 616 | struct parport_state *s) |
| 617 | { |
| 618 | struct mos7715_parport *mos_parport; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 619 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 620 | spin_lock(&release_lock); |
| 621 | mos_parport = pp->private_data; |
| 622 | if (unlikely(mos_parport == NULL)) { /* release called */ |
| 623 | spin_unlock(&release_lock); |
| 624 | return; |
| 625 | } |
| 626 | write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR); |
| 627 | write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR); |
| 628 | spin_unlock(&release_lock); |
| 629 | } |
| 630 | |
| 631 | static size_t parport_mos7715_write_compat(struct parport *pp, |
| 632 | const void *buffer, |
| 633 | size_t len, int flags) |
| 634 | { |
| 635 | int retval; |
| 636 | struct mos7715_parport *mos_parport = pp->private_data; |
| 637 | int actual_len; |
Greg Kroah-Hartman | ca09907 | 2012-05-03 16:44:31 -0700 | [diff] [blame] | 638 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 639 | if (parport_prologue(pp) < 0) |
| 640 | return 0; |
| 641 | mos7715_change_mode(mos_parport, PPF); |
| 642 | retval = usb_bulk_msg(mos_parport->serial->dev, |
| 643 | usb_sndbulkpipe(mos_parport->serial->dev, 2), |
| 644 | (void *)buffer, len, &actual_len, |
| 645 | MOS_WDR_TIMEOUT); |
| 646 | parport_epilogue(pp); |
| 647 | if (retval) { |
| 648 | dev_err(&mos_parport->serial->dev->dev, |
| 649 | "mos7720: usb_bulk_msg() failed: %d", retval); |
| 650 | return 0; |
| 651 | } |
| 652 | return actual_len; |
| 653 | } |
| 654 | |
| 655 | static struct parport_operations parport_mos7715_ops = { |
| 656 | .owner = THIS_MODULE, |
| 657 | .write_data = parport_mos7715_write_data, |
| 658 | .read_data = parport_mos7715_read_data, |
| 659 | |
| 660 | .write_control = parport_mos7715_write_control, |
| 661 | .read_control = parport_mos7715_read_control, |
| 662 | .frob_control = parport_mos7715_frob_control, |
| 663 | |
| 664 | .read_status = parport_mos7715_read_status, |
| 665 | |
| 666 | .enable_irq = parport_mos7715_enable_irq, |
| 667 | .disable_irq = parport_mos7715_disable_irq, |
| 668 | |
| 669 | .data_forward = parport_mos7715_data_forward, |
| 670 | .data_reverse = parport_mos7715_data_reverse, |
| 671 | |
| 672 | .init_state = parport_mos7715_init_state, |
| 673 | .save_state = parport_mos7715_save_state, |
| 674 | .restore_state = parport_mos7715_restore_state, |
| 675 | |
| 676 | .compat_write_data = parport_mos7715_write_compat, |
| 677 | |
| 678 | .nibble_read_data = parport_ieee1284_read_nibble, |
| 679 | .byte_read_data = parport_ieee1284_read_byte, |
| 680 | }; |
| 681 | |
| 682 | /* |
| 683 | * Allocate and initialize parallel port control struct, initialize |
| 684 | * the parallel port hardware device, and register with the parport subsystem. |
| 685 | */ |
| 686 | static int mos7715_parport_init(struct usb_serial *serial) |
| 687 | { |
| 688 | struct mos7715_parport *mos_parport; |
| 689 | |
| 690 | /* allocate and initialize parallel port control struct */ |
| 691 | mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL); |
| 692 | if (mos_parport == NULL) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 693 | dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 694 | return -ENOMEM; |
| 695 | } |
| 696 | mos_parport->msg_pending = false; |
| 697 | kref_init(&mos_parport->ref_count); |
| 698 | spin_lock_init(&mos_parport->listlock); |
| 699 | INIT_LIST_HEAD(&mos_parport->active_urbs); |
| 700 | INIT_LIST_HEAD(&mos_parport->deferred_urbs); |
| 701 | usb_set_serial_data(serial, mos_parport); /* hijack private pointer */ |
| 702 | mos_parport->serial = serial; |
| 703 | tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs, |
| 704 | (unsigned long) mos_parport); |
| 705 | init_completion(&mos_parport->syncmsg_compl); |
| 706 | |
| 707 | /* cycle parallel port reset bit */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 708 | write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80); |
| 709 | write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 710 | |
| 711 | /* initialize device registers */ |
| 712 | mos_parport->shadowDCR = DCR_INIT_VAL; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 713 | write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 714 | mos_parport->shadowECR = ECR_INIT_VAL; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 715 | write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR); |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 716 | |
| 717 | /* register with parport core */ |
| 718 | mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE, |
| 719 | PARPORT_DMA_NONE, |
| 720 | &parport_mos7715_ops); |
| 721 | if (mos_parport->pp == NULL) { |
| 722 | dev_err(&serial->interface->dev, |
| 723 | "Could not register parport\n"); |
| 724 | kref_put(&mos_parport->ref_count, destroy_mos_parport); |
| 725 | return -EIO; |
| 726 | } |
| 727 | mos_parport->pp->private_data = mos_parport; |
| 728 | mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP; |
| 729 | mos_parport->pp->dev = &serial->interface->dev; |
| 730 | parport_announce_port(mos_parport->pp); |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | * mos7720_interrupt_callback |
| 738 | * this is the callback function for when we have received data on the |
| 739 | * interrupt endpoint. |
| 740 | */ |
| 741 | static void mos7720_interrupt_callback(struct urb *urb) |
| 742 | { |
| 743 | int result; |
| 744 | int length; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 745 | int status = urb->status; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 746 | struct device *dev = &urb->dev->dev; |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 747 | __u8 *data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 748 | __u8 sp1; |
| 749 | __u8 sp2; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 750 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 751 | switch (status) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 752 | case 0: |
| 753 | /* success */ |
| 754 | break; |
| 755 | case -ECONNRESET: |
| 756 | case -ENOENT: |
| 757 | case -ESHUTDOWN: |
| 758 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 759 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 760 | return; |
| 761 | default: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 762 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 763 | goto exit; |
| 764 | } |
| 765 | |
| 766 | length = urb->actual_length; |
| 767 | data = urb->transfer_buffer; |
| 768 | |
| 769 | /* Moschip get 4 bytes |
| 770 | * Byte 1 IIR Port 1 (port.number is 0) |
| 771 | * Byte 2 IIR Port 2 (port.number is 1) |
| 772 | * Byte 3 -------------- |
| 773 | * Byte 4 FIFO status for both */ |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 774 | |
| 775 | /* the above description is inverted |
| 776 | * oneukum 2007-03-14 */ |
| 777 | |
| 778 | if (unlikely(length != 4)) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 779 | dev_dbg(dev, "Wrong data !!!\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 780 | return; |
| 781 | } |
| 782 | |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 783 | sp1 = data[3]; |
| 784 | sp2 = data[2]; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 785 | |
Oliver Neukum | 325b70c | 2007-03-19 13:58:29 +0100 | [diff] [blame] | 786 | if ((sp1 | sp2) & 0x01) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 787 | /* No Interrupt Pending in both the ports */ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 788 | dev_dbg(dev, "No Interrupt !!!\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 789 | } else { |
| 790 | switch (sp1 & 0x0f) { |
| 791 | case SERIAL_IIR_RLS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 792 | dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 793 | break; |
| 794 | case SERIAL_IIR_CTI: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 795 | dev_dbg(dev, "Serial Port 1: Receiver time out\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 796 | break; |
| 797 | case SERIAL_IIR_MS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 798 | /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | |
| 802 | switch (sp2 & 0x0f) { |
| 803 | case SERIAL_IIR_RLS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 804 | dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 805 | break; |
| 806 | case SERIAL_IIR_CTI: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 807 | dev_dbg(dev, "Serial Port 2: Receiver time out\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 808 | break; |
| 809 | case SERIAL_IIR_MS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 810 | /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | exit: |
| 816 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 817 | if (result) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 818 | dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 822 | * mos7715_interrupt_callback |
| 823 | * this is the 7715's callback function for when we have received data on |
| 824 | * the interrupt endpoint. |
| 825 | */ |
| 826 | static void mos7715_interrupt_callback(struct urb *urb) |
| 827 | { |
| 828 | int result; |
| 829 | int length; |
| 830 | int status = urb->status; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 831 | struct device *dev = &urb->dev->dev; |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 832 | __u8 *data; |
| 833 | __u8 iir; |
| 834 | |
| 835 | switch (status) { |
| 836 | case 0: |
| 837 | /* success */ |
| 838 | break; |
| 839 | case -ECONNRESET: |
| 840 | case -ENOENT: |
| 841 | case -ESHUTDOWN: |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 842 | case -ENODEV: |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 843 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 844 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 845 | return; |
| 846 | default: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 847 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 848 | goto exit; |
| 849 | } |
| 850 | |
| 851 | length = urb->actual_length; |
| 852 | data = urb->transfer_buffer; |
| 853 | |
| 854 | /* Structure of data from 7715 device: |
| 855 | * Byte 1: IIR serial Port |
| 856 | * Byte 2: unused |
| 857 | * Byte 2: DSR parallel port |
| 858 | * Byte 4: FIFO status for both */ |
| 859 | |
| 860 | if (unlikely(length != 4)) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 861 | dev_dbg(dev, "Wrong data !!!\n"); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | |
| 865 | iir = data[0]; |
| 866 | if (!(iir & 0x01)) { /* serial port interrupt pending */ |
| 867 | switch (iir & 0x0f) { |
| 868 | case SERIAL_IIR_RLS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 869 | dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n"); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 870 | break; |
| 871 | case SERIAL_IIR_CTI: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 872 | dev_dbg(dev, "Serial Port: Receiver time out\n"); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 873 | break; |
| 874 | case SERIAL_IIR_MS: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 875 | /* dev_dbg(dev, "Serial Port: Modem status change\n"); */ |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | } |
| 879 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 880 | #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT |
| 881 | { /* update local copy of DSR reg */ |
| 882 | struct usb_serial_port *port = urb->context; |
| 883 | struct mos7715_parport *mos_parport = port->serial->private; |
| 884 | if (unlikely(mos_parport == NULL)) |
| 885 | return; |
| 886 | atomic_set(&mos_parport->shadowDSR, data[2]); |
| 887 | } |
| 888 | #endif |
| 889 | |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 890 | exit: |
| 891 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 892 | if (result) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 893 | dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 897 | * mos7720_bulk_in_callback |
| 898 | * this is the callback function for when we have received data on the |
| 899 | * bulk in endpoint. |
| 900 | */ |
| 901 | static void mos7720_bulk_in_callback(struct urb *urb) |
| 902 | { |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 903 | int retval; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 904 | unsigned char *data ; |
| 905 | struct usb_serial_port *port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 906 | struct tty_struct *tty; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 907 | int status = urb->status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 908 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 909 | if (status) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 910 | dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 914 | port = urb->context; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 915 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 916 | dev_dbg(&port->dev, "Entering...%s\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 917 | |
| 918 | data = urb->transfer_buffer; |
| 919 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 920 | tty = tty_port_tty_get(&port->port); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 921 | if (tty && urb->actual_length) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 922 | tty_insert_flip_string(tty, data, urb->actual_length); |
| 923 | tty_flip_buffer_push(tty); |
| 924 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 925 | tty_kref_put(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 926 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 927 | if (port->read_urb->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 928 | retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 929 | if (retval) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 930 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * mos7720_bulk_out_data_callback |
| 936 | * this is the callback function for when we have finished sending serial |
| 937 | * data on the bulk out endpoint. |
| 938 | */ |
| 939 | static void mos7720_bulk_out_data_callback(struct urb *urb) |
| 940 | { |
| 941 | struct moschip_port *mos7720_port; |
| 942 | struct tty_struct *tty; |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 943 | int status = urb->status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 944 | |
Greg Kroah-Hartman | 8110598 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 945 | if (status) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 946 | dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 947 | return; |
| 948 | } |
| 949 | |
| 950 | mos7720_port = urb->context; |
| 951 | if (!mos7720_port) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 952 | dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 953 | return ; |
| 954 | } |
| 955 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 956 | tty = tty_port_tty_get(&mos7720_port->port->port); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 957 | |
Jiri Slaby | b963a84 | 2007-02-10 01:44:55 -0800 | [diff] [blame] | 958 | if (tty && mos7720_port->open) |
| 959 | tty_wakeup(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 960 | tty_kref_put(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 964 | * mos77xx_probe |
| 965 | * this function installs the appropriate read interrupt endpoint callback |
| 966 | * depending on whether the device is a 7720 or 7715, thus avoiding costly |
| 967 | * run-time checks in the high-frequency callback routine itself. |
| 968 | */ |
| 969 | static int mos77xx_probe(struct usb_serial *serial, |
| 970 | const struct usb_device_id *id) |
| 971 | { |
| 972 | if (id->idProduct == MOSCHIP_DEVICE_ID_7715) |
| 973 | moschip7720_2port_driver.read_int_callback = |
| 974 | mos7715_interrupt_callback; |
| 975 | else |
| 976 | moschip7720_2port_driver.read_int_callback = |
| 977 | mos7720_interrupt_callback; |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int mos77xx_calc_num_ports(struct usb_serial *serial) |
| 983 | { |
| 984 | u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); |
| 985 | if (product == MOSCHIP_DEVICE_ID_7715) |
| 986 | return 1; |
| 987 | |
| 988 | return 2; |
| 989 | } |
| 990 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 991 | static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 992 | { |
| 993 | struct usb_serial *serial; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 994 | struct urb *urb; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 995 | struct moschip_port *mos7720_port; |
| 996 | int response; |
| 997 | int port_number; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 998 | __u8 data; |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 999 | int allocated_urbs = 0; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1000 | int j; |
| 1001 | |
| 1002 | serial = port->serial; |
| 1003 | |
| 1004 | mos7720_port = usb_get_serial_port_data(port); |
| 1005 | if (mos7720_port == NULL) |
| 1006 | return -ENODEV; |
| 1007 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1008 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 1009 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 1010 | |
| 1011 | /* Initialising the write urb pool */ |
| 1012 | for (j = 0; j < NUM_URBS; ++j) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1013 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1014 | mos7720_port->write_urb_pool[j] = urb; |
| 1015 | |
| 1016 | if (urb == NULL) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1017 | dev_err(&port->dev, "No more urbs???\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1018 | continue; |
| 1019 | } |
| 1020 | |
| 1021 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 1022 | GFP_KERNEL); |
| 1023 | if (!urb->transfer_buffer) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1024 | dev_err(&port->dev, |
| 1025 | "%s-out of memory for urb buffers.\n", |
| 1026 | __func__); |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 1027 | usb_free_urb(mos7720_port->write_urb_pool[j]); |
| 1028 | mos7720_port->write_urb_pool[j] = NULL; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1029 | continue; |
| 1030 | } |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 1031 | allocated_urbs++; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Oliver Neukum | fe4b65e | 2007-03-14 15:22:25 +0100 | [diff] [blame] | 1034 | if (!allocated_urbs) |
| 1035 | return -ENOMEM; |
| 1036 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1037 | /* Initialize MCS7720 -- Write Init values to corresponding Registers |
| 1038 | * |
| 1039 | * Register Index |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1040 | * 0 : THR/RHR |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1041 | * 1 : IER |
| 1042 | * 2 : FCR |
| 1043 | * 3 : LCR |
| 1044 | * 4 : MCR |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1045 | * 5 : LSR |
| 1046 | * 6 : MSR |
| 1047 | * 7 : SPR |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1048 | * |
| 1049 | * 0x08 : SP1/2 Control Reg |
| 1050 | */ |
| 1051 | port_number = port->number - port->serial->minor; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1052 | read_mos_reg(serial, port_number, LSR, &data); |
| 1053 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1054 | dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1055 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1056 | write_mos_reg(serial, dummy, SP1_REG, 0x02); |
| 1057 | write_mos_reg(serial, dummy, SP2_REG, 0x02); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1058 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1059 | write_mos_reg(serial, port_number, IER, 0x00); |
| 1060 | write_mos_reg(serial, port_number, FCR, 0x00); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1061 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1062 | write_mos_reg(serial, port_number, FCR, 0xcf); |
| 1063 | mos7720_port->shadowLCR = 0x03; |
| 1064 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
| 1065 | mos7720_port->shadowMCR = 0x0b; |
| 1066 | write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1067 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1068 | write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00); |
| 1069 | read_mos_reg(serial, dummy, SP_CONTROL_REG, &data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1070 | data = data | (port->number - port->serial->minor + 1); |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1071 | write_mos_reg(serial, dummy, SP_CONTROL_REG, data); |
| 1072 | mos7720_port->shadowLCR = 0x83; |
| 1073 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
| 1074 | write_mos_reg(serial, port_number, THR, 0x0c); |
| 1075 | write_mos_reg(serial, port_number, IER, 0x00); |
| 1076 | mos7720_port->shadowLCR = 0x03; |
| 1077 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
| 1078 | write_mos_reg(serial, port_number, IER, 0x0c); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1079 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1080 | response = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 1081 | if (response) |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1082 | dev_err(&port->dev, "%s - Error %d submitting read urb\n", |
| 1083 | __func__, response); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1084 | |
| 1085 | /* initialize our icount structure */ |
| 1086 | memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount)); |
| 1087 | |
| 1088 | /* initialize our port settings */ |
| 1089 | mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */ |
| 1090 | |
| 1091 | /* send a open port command */ |
| 1092 | mos7720_port->open = 1; |
| 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * mos7720_chars_in_buffer |
| 1099 | * this function is called by the tty driver when it wants to know how many |
| 1100 | * bytes of data we currently have outstanding in the port (data that has |
| 1101 | * been written, but hasn't made it out the port yet) |
| 1102 | * If successful, we return the number of bytes left to be written in the |
| 1103 | * system, |
| 1104 | * Otherwise we return a negative error number. |
| 1105 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1106 | static int mos7720_chars_in_buffer(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1107 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1108 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1109 | int i; |
| 1110 | int chars = 0; |
| 1111 | struct moschip_port *mos7720_port; |
| 1112 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1113 | mos7720_port = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1114 | if (mos7720_port == NULL) |
Alan Cox | 23198fd | 2009-07-20 16:05:27 +0100 | [diff] [blame] | 1115 | return 0; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1116 | |
| 1117 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1118 | if (mos7720_port->write_urb_pool[i] && |
| 1119 | mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1120 | chars += URB_TRANSFER_BUFFER_SIZE; |
| 1121 | } |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1122 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1123 | return chars; |
| 1124 | } |
| 1125 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 1126 | static void mos7720_close(struct usb_serial_port *port) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1127 | { |
| 1128 | struct usb_serial *serial; |
| 1129 | struct moschip_port *mos7720_port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1130 | int j; |
| 1131 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1132 | serial = port->serial; |
| 1133 | |
| 1134 | mos7720_port = usb_get_serial_port_data(port); |
| 1135 | if (mos7720_port == NULL) |
| 1136 | return; |
| 1137 | |
| 1138 | for (j = 0; j < NUM_URBS; ++j) |
| 1139 | usb_kill_urb(mos7720_port->write_urb_pool[j]); |
| 1140 | |
| 1141 | /* Freeing Write URBs */ |
| 1142 | for (j = 0; j < NUM_URBS; ++j) { |
| 1143 | if (mos7720_port->write_urb_pool[j]) { |
| 1144 | kfree(mos7720_port->write_urb_pool[j]->transfer_buffer); |
| 1145 | usb_free_urb(mos7720_port->write_urb_pool[j]); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /* While closing port, shutdown all bulk read, write * |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 1150 | * and interrupt read if they exists, otherwise nop */ |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 1151 | usb_kill_urb(port->write_urb); |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 1152 | usb_kill_urb(port->read_urb); |
| 1153 | |
| 1154 | mutex_lock(&serial->disc_mutex); |
| 1155 | /* these commands must not be issued if the device has |
| 1156 | * been disconnected */ |
| 1157 | if (!serial->disconnected) { |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1158 | write_mos_reg(serial, port->number - port->serial->minor, |
| 1159 | MCR, 0x00); |
| 1160 | write_mos_reg(serial, port->number - port->serial->minor, |
| 1161 | IER, 0x00); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1162 | } |
Oliver Neukum | a1cd7e9 | 2008-01-16 17:18:52 +0100 | [diff] [blame] | 1163 | mutex_unlock(&serial->disc_mutex); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1164 | mos7720_port->open = 0; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1167 | static void mos7720_break(struct tty_struct *tty, int break_state) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1168 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1169 | struct usb_serial_port *port = tty->driver_data; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1170 | unsigned char data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1171 | struct usb_serial *serial; |
| 1172 | struct moschip_port *mos7720_port; |
| 1173 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1174 | serial = port->serial; |
| 1175 | |
| 1176 | mos7720_port = usb_get_serial_port_data(port); |
| 1177 | if (mos7720_port == NULL) |
| 1178 | return; |
| 1179 | |
| 1180 | if (break_state == -1) |
| 1181 | data = mos7720_port->shadowLCR | UART_LCR_SBC; |
| 1182 | else |
| 1183 | data = mos7720_port->shadowLCR & ~UART_LCR_SBC; |
| 1184 | |
| 1185 | mos7720_port->shadowLCR = data; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1186 | write_mos_reg(serial, port->number - port->serial->minor, |
| 1187 | LCR, mos7720_port->shadowLCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * mos7720_write_room |
| 1192 | * this function is called by the tty driver when it wants to know how many |
| 1193 | * bytes of data we can accept for a specific port. |
| 1194 | * If successful, we return the amount of room that we have for this port |
| 1195 | * Otherwise we return a negative error number. |
| 1196 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1197 | static int mos7720_write_room(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1198 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1199 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1200 | struct moschip_port *mos7720_port; |
| 1201 | int room = 0; |
| 1202 | int i; |
| 1203 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1204 | mos7720_port = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1205 | if (mos7720_port == NULL) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1206 | return -ENODEV; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1207 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 1208 | /* FIXME: Locking */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1209 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1210 | if (mos7720_port->write_urb_pool[i] && |
| 1211 | mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1212 | room += URB_TRANSFER_BUFFER_SIZE; |
| 1213 | } |
| 1214 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1215 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1216 | return room; |
| 1217 | } |
| 1218 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1219 | static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 1220 | const unsigned char *data, int count) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1221 | { |
| 1222 | int status; |
| 1223 | int i; |
| 1224 | int bytes_sent = 0; |
| 1225 | int transfer_size; |
| 1226 | |
| 1227 | struct moschip_port *mos7720_port; |
| 1228 | struct usb_serial *serial; |
| 1229 | struct urb *urb; |
| 1230 | const unsigned char *current_position = data; |
| 1231 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1232 | serial = port->serial; |
| 1233 | |
| 1234 | mos7720_port = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1235 | if (mos7720_port == NULL) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1236 | return -ENODEV; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1237 | |
| 1238 | /* try to find a free urb in the list */ |
| 1239 | urb = NULL; |
| 1240 | |
| 1241 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1242 | if (mos7720_port->write_urb_pool[i] && |
| 1243 | mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1244 | urb = mos7720_port->write_urb_pool[i]; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1245 | dev_dbg(&port->dev, "URB:%d\n", i); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (urb == NULL) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1251 | dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1252 | goto exit; |
| 1253 | } |
| 1254 | |
| 1255 | if (urb->transfer_buffer == NULL) { |
| 1256 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 1257 | GFP_KERNEL); |
| 1258 | if (urb->transfer_buffer == NULL) { |
Johan Hovold | 22a416c | 2012-02-10 13:20:51 +0100 | [diff] [blame] | 1259 | dev_err_console(port, "%s no more kernel memory...\n", |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1260 | __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1261 | goto exit; |
| 1262 | } |
| 1263 | } |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1264 | transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1265 | |
| 1266 | memcpy(urb->transfer_buffer, current_position, transfer_size); |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 1267 | usb_serial_debug_data(&port->dev, __func__, transfer_size, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1268 | urb->transfer_buffer); |
| 1269 | |
| 1270 | /* fill urb with data and submit */ |
| 1271 | usb_fill_bulk_urb(urb, serial->dev, |
| 1272 | usb_sndbulkpipe(serial->dev, |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1273 | port->bulk_out_endpointAddress), |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1274 | urb->transfer_buffer, transfer_size, |
| 1275 | mos7720_bulk_out_data_callback, mos7720_port); |
| 1276 | |
| 1277 | /* send it down the pipe */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1278 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1279 | if (status) { |
Johan Hovold | 22a416c | 2012-02-10 13:20:51 +0100 | [diff] [blame] | 1280 | dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1281 | "with status = %d\n", __func__, status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1282 | bytes_sent = status; |
| 1283 | goto exit; |
| 1284 | } |
| 1285 | bytes_sent = transfer_size; |
| 1286 | |
| 1287 | exit: |
| 1288 | return bytes_sent; |
| 1289 | } |
| 1290 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1291 | static void mos7720_throttle(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1292 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1293 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1294 | struct moschip_port *mos7720_port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1295 | int status; |
| 1296 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1297 | mos7720_port = usb_get_serial_port_data(port); |
| 1298 | |
| 1299 | if (mos7720_port == NULL) |
| 1300 | return; |
| 1301 | |
| 1302 | if (!mos7720_port->open) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1303 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1304 | return; |
| 1305 | } |
| 1306 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1307 | /* if we are implementing XON/XOFF, send the stop character */ |
| 1308 | if (I_IXOFF(tty)) { |
| 1309 | unsigned char stop_char = STOP_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1310 | status = mos7720_write(tty, port, &stop_char, 1); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1311 | if (status <= 0) |
| 1312 | return; |
| 1313 | } |
| 1314 | |
| 1315 | /* if we are implementing RTS/CTS, toggle that line */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1316 | if (tty->termios.c_cflag & CRTSCTS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1317 | mos7720_port->shadowMCR &= ~UART_MCR_RTS; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1318 | write_mos_reg(port->serial, port->number - port->serial->minor, |
| 1319 | MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1320 | if (status != 0) |
| 1321 | return; |
| 1322 | } |
| 1323 | } |
| 1324 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1325 | static void mos7720_unthrottle(struct tty_struct *tty) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1326 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1327 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1328 | struct moschip_port *mos7720_port = usb_get_serial_port_data(port); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1329 | int status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1330 | |
| 1331 | if (mos7720_port == NULL) |
| 1332 | return; |
| 1333 | |
| 1334 | if (!mos7720_port->open) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1335 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1336 | return; |
| 1337 | } |
| 1338 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1339 | /* if we are implementing XON/XOFF, send the start character */ |
| 1340 | if (I_IXOFF(tty)) { |
| 1341 | unsigned char start_char = START_CHAR(tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1342 | status = mos7720_write(tty, port, &start_char, 1); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1343 | if (status <= 0) |
| 1344 | return; |
| 1345 | } |
| 1346 | |
| 1347 | /* if we are implementing RTS/CTS, toggle that line */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1348 | if (tty->termios.c_cflag & CRTSCTS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1349 | mos7720_port->shadowMCR |= UART_MCR_RTS; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1350 | write_mos_reg(port->serial, port->number - port->serial->minor, |
| 1351 | MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1352 | if (status != 0) |
| 1353 | return; |
| 1354 | } |
| 1355 | } |
| 1356 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 1357 | /* FIXME: this function does not work */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1358 | static int set_higher_rates(struct moschip_port *mos7720_port, |
| 1359 | unsigned int baud) |
| 1360 | { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1361 | struct usb_serial_port *port; |
| 1362 | struct usb_serial *serial; |
| 1363 | int port_number; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1364 | enum mos_regs sp_reg; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1365 | if (mos7720_port == NULL) |
| 1366 | return -EINVAL; |
| 1367 | |
| 1368 | port = mos7720_port->port; |
| 1369 | serial = port->serial; |
| 1370 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1371 | /*********************************************** |
| 1372 | * Init Sequence for higher rates |
| 1373 | ***********************************************/ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1374 | dev_dbg(&port->dev, "Sending Setting Commands ..........\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1375 | port_number = port->number - port->serial->minor; |
| 1376 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1377 | write_mos_reg(serial, port_number, IER, 0x00); |
| 1378 | write_mos_reg(serial, port_number, FCR, 0x00); |
| 1379 | write_mos_reg(serial, port_number, FCR, 0xcf); |
| 1380 | mos7720_port->shadowMCR = 0x0b; |
| 1381 | write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); |
| 1382 | write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1383 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1384 | /*********************************************** |
| 1385 | * Set for higher rates * |
| 1386 | ***********************************************/ |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 1387 | /* writing baud rate verbatum into uart clock field clearly not right */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1388 | if (port_number == 0) |
| 1389 | sp_reg = SP1_REG; |
| 1390 | else |
| 1391 | sp_reg = SP2_REG; |
| 1392 | write_mos_reg(serial, dummy, sp_reg, baud * 0x10); |
| 1393 | write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03); |
| 1394 | mos7720_port->shadowMCR = 0x2b; |
| 1395 | write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1396 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1397 | /*********************************************** |
| 1398 | * Set DLL/DLM |
| 1399 | ***********************************************/ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1400 | mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; |
| 1401 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
| 1402 | write_mos_reg(serial, port_number, DLL, 0x01); |
| 1403 | write_mos_reg(serial, port_number, DLM, 0x00); |
| 1404 | mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; |
| 1405 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | /* baud rate information */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1411 | struct divisor_table_entry { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1412 | __u32 baudrate; |
| 1413 | __u16 divisor; |
| 1414 | }; |
| 1415 | |
| 1416 | /* Define table of divisors for moschip 7720 hardware * |
| 1417 | * These assume a 3.6864MHz crystal, the standard /16, and * |
| 1418 | * MCR.7 = 0. */ |
| 1419 | static struct divisor_table_entry divisor_table[] = { |
| 1420 | { 50, 2304}, |
| 1421 | { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */ |
| 1422 | { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */ |
| 1423 | { 150, 768}, |
| 1424 | { 300, 384}, |
| 1425 | { 600, 192}, |
| 1426 | { 1200, 96}, |
| 1427 | { 1800, 64}, |
| 1428 | { 2400, 48}, |
| 1429 | { 4800, 24}, |
| 1430 | { 7200, 16}, |
| 1431 | { 9600, 12}, |
| 1432 | { 19200, 6}, |
| 1433 | { 38400, 3}, |
| 1434 | { 57600, 2}, |
| 1435 | { 115200, 1}, |
| 1436 | }; |
| 1437 | |
| 1438 | /***************************************************************************** |
| 1439 | * calc_baud_rate_divisor |
| 1440 | * this function calculates the proper baud rate divisor for the specified |
| 1441 | * baud rate. |
| 1442 | *****************************************************************************/ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1443 | static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1444 | { |
| 1445 | int i; |
| 1446 | __u16 custom; |
| 1447 | __u16 round1; |
| 1448 | __u16 round; |
| 1449 | |
| 1450 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1451 | dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1452 | |
| 1453 | for (i = 0; i < ARRAY_SIZE(divisor_table); i++) { |
| 1454 | if (divisor_table[i].baudrate == baudrate) { |
| 1455 | *divisor = divisor_table[i].divisor; |
| 1456 | return 0; |
| 1457 | } |
| 1458 | } |
| 1459 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1460 | /* After trying for all the standard baud rates * |
| 1461 | * Try calculating the divisor for this baud rate */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1462 | if (baudrate > 75 && baudrate < 230400) { |
| 1463 | /* get the divisor */ |
| 1464 | custom = (__u16)(230400L / baudrate); |
| 1465 | |
| 1466 | /* Check for round off */ |
| 1467 | round1 = (__u16)(2304000L / baudrate); |
| 1468 | round = (__u16)(round1 - (custom * 10)); |
| 1469 | if (round > 4) |
| 1470 | custom++; |
| 1471 | *divisor = custom; |
| 1472 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1473 | dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1474 | return 0; |
| 1475 | } |
| 1476 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1477 | dev_dbg(&port->dev, "Baud calculation Failed...\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1478 | return -EINVAL; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * send_cmd_write_baud_rate |
| 1483 | * this function sends the proper command to change the baud rate of the |
| 1484 | * specified port. |
| 1485 | */ |
| 1486 | static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port, |
| 1487 | int baudrate) |
| 1488 | { |
| 1489 | struct usb_serial_port *port; |
| 1490 | struct usb_serial *serial; |
| 1491 | int divisor; |
| 1492 | int status; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1493 | unsigned char number; |
| 1494 | |
| 1495 | if (mos7720_port == NULL) |
| 1496 | return -1; |
| 1497 | |
| 1498 | port = mos7720_port->port; |
| 1499 | serial = port->serial; |
| 1500 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1501 | number = port->number - port->serial->minor; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1502 | dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1503 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1504 | /* Calculate the Divisor */ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1505 | status = calc_baud_rate_divisor(port, baudrate, &divisor); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1506 | if (status) { |
Greg Kroah-Hartman | 194343d | 2008-08-20 16:56:34 -0700 | [diff] [blame] | 1507 | dev_err(&port->dev, "%s - bad baud rate\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1508 | return status; |
| 1509 | } |
| 1510 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1511 | /* Enable access to divisor latch */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1512 | mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; |
| 1513 | write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1514 | |
| 1515 | /* Write the divisor */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1516 | write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff)); |
| 1517 | write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8)); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1518 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1519 | /* Disable access to divisor latch */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1520 | mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; |
| 1521 | write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1522 | |
| 1523 | return status; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * change_port_settings |
| 1528 | * This routine is called to set the UART on the device to match |
| 1529 | * the specified new settings. |
| 1530 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1531 | static void change_port_settings(struct tty_struct *tty, |
| 1532 | struct moschip_port *mos7720_port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1533 | struct ktermios *old_termios) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1534 | { |
| 1535 | struct usb_serial_port *port; |
| 1536 | struct usb_serial *serial; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1537 | int baud; |
| 1538 | unsigned cflag; |
| 1539 | unsigned iflag; |
| 1540 | __u8 mask = 0xff; |
| 1541 | __u8 lData; |
| 1542 | __u8 lParity; |
| 1543 | __u8 lStop; |
| 1544 | int status; |
| 1545 | int port_number; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1546 | |
| 1547 | if (mos7720_port == NULL) |
| 1548 | return ; |
| 1549 | |
| 1550 | port = mos7720_port->port; |
| 1551 | serial = port->serial; |
| 1552 | port_number = port->number - port->serial->minor; |
| 1553 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1554 | if (!mos7720_port->open) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1555 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1556 | return; |
| 1557 | } |
| 1558 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1559 | lData = UART_LCR_WLEN8; |
| 1560 | lStop = 0x00; /* 1 stop bit */ |
| 1561 | lParity = 0x00; /* No parity */ |
| 1562 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1563 | cflag = tty->termios.c_cflag; |
| 1564 | iflag = tty->termios.c_iflag; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* Change the number of bits */ |
| 1567 | switch (cflag & CSIZE) { |
| 1568 | case CS5: |
| 1569 | lData = UART_LCR_WLEN5; |
| 1570 | mask = 0x1f; |
| 1571 | break; |
| 1572 | |
| 1573 | case CS6: |
| 1574 | lData = UART_LCR_WLEN6; |
| 1575 | mask = 0x3f; |
| 1576 | break; |
| 1577 | |
| 1578 | case CS7: |
| 1579 | lData = UART_LCR_WLEN7; |
| 1580 | mask = 0x7f; |
| 1581 | break; |
| 1582 | default: |
| 1583 | case CS8: |
| 1584 | lData = UART_LCR_WLEN8; |
| 1585 | break; |
| 1586 | } |
| 1587 | |
| 1588 | /* Change the Parity bit */ |
| 1589 | if (cflag & PARENB) { |
| 1590 | if (cflag & PARODD) { |
| 1591 | lParity = UART_LCR_PARITY; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1592 | dev_dbg(&port->dev, "%s - parity = odd\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1593 | } else { |
| 1594 | lParity = (UART_LCR_EPAR | UART_LCR_PARITY); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1595 | dev_dbg(&port->dev, "%s - parity = even\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | } else { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1599 | dev_dbg(&port->dev, "%s - parity = none\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | if (cflag & CMSPAR) |
| 1603 | lParity = lParity | 0x20; |
| 1604 | |
| 1605 | /* Change the Stop bit */ |
| 1606 | if (cflag & CSTOPB) { |
| 1607 | lStop = UART_LCR_STOP; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1608 | dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1609 | } else { |
| 1610 | lStop = 0x00; |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1611 | dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ |
| 1615 | #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ |
| 1616 | #define LCR_PAR_MASK 0x38 /* Mask for parity field */ |
| 1617 | |
| 1618 | /* Update the LCR with the correct value */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1619 | mos7720_port->shadowLCR &= |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1620 | ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1621 | mos7720_port->shadowLCR |= (lData | lParity | lStop); |
| 1622 | |
| 1623 | |
| 1624 | /* Disable Interrupts */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1625 | write_mos_reg(serial, port_number, IER, 0x00); |
| 1626 | write_mos_reg(serial, port_number, FCR, 0x00); |
| 1627 | write_mos_reg(serial, port_number, FCR, 0xcf); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1628 | |
| 1629 | /* Send the updated LCR value to the mos7720 */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1630 | write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); |
| 1631 | mos7720_port->shadowMCR = 0x0b; |
| 1632 | write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1633 | |
| 1634 | /* set up the MCR register and send it to the mos7720 */ |
| 1635 | mos7720_port->shadowMCR = UART_MCR_OUT2; |
| 1636 | if (cflag & CBAUD) |
| 1637 | mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS); |
| 1638 | |
| 1639 | if (cflag & CRTSCTS) { |
| 1640 | mos7720_port->shadowMCR |= (UART_MCR_XONANY); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1641 | /* To set hardware flow control to the specified * |
| 1642 | * serial port, in SP1/2_CONTROL_REG */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1643 | if (port->number) |
| 1644 | write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01); |
| 1645 | else |
| 1646 | write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1647 | |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1648 | } else |
| 1649 | mos7720_port->shadowMCR &= ~(UART_MCR_XONANY); |
| 1650 | |
| 1651 | write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1652 | |
| 1653 | /* Determine divisor based on baud rate */ |
| 1654 | baud = tty_get_baud_rate(tty); |
| 1655 | if (!baud) { |
| 1656 | /* pick a default, any default... */ |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1657 | dev_dbg(&port->dev, "Picked default baud...\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1658 | baud = 9600; |
| 1659 | } |
| 1660 | |
| 1661 | if (baud >= 230400) { |
| 1662 | set_higher_rates(mos7720_port, baud); |
| 1663 | /* Enable Interrupts */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1664 | write_mos_reg(serial, port_number, IER, 0x0c); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1665 | return; |
| 1666 | } |
| 1667 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1668 | dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1669 | status = send_cmd_write_baud_rate(mos7720_port, baud); |
Alan Cox | 65d063ab | 2008-01-03 17:01:18 +0000 | [diff] [blame] | 1670 | /* FIXME: needs to write actual resulting baud back not just |
| 1671 | blindly do so */ |
| 1672 | if (cflag & CBAUD) |
| 1673 | tty_encode_baud_rate(tty, baud, baud); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1674 | /* Enable Interrupts */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1675 | write_mos_reg(serial, port_number, IER, 0x0c); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1676 | |
| 1677 | if (port->read_urb->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1678 | status = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 1679 | if (status) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1680 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1681 | } |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * mos7720_set_termios |
| 1686 | * this function is called by the tty driver when it wants to change the |
| 1687 | * termios structure. |
| 1688 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1689 | static void mos7720_set_termios(struct tty_struct *tty, |
| 1690 | struct usb_serial_port *port, struct ktermios *old_termios) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1691 | { |
| 1692 | int status; |
| 1693 | unsigned int cflag; |
| 1694 | struct usb_serial *serial; |
| 1695 | struct moschip_port *mos7720_port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1696 | |
| 1697 | serial = port->serial; |
| 1698 | |
| 1699 | mos7720_port = usb_get_serial_port_data(port); |
| 1700 | |
| 1701 | if (mos7720_port == NULL) |
| 1702 | return; |
| 1703 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1704 | if (!mos7720_port->open) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1705 | dev_dbg(&port->dev, "%s - port not opened\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1706 | return; |
| 1707 | } |
| 1708 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1709 | dev_dbg(&port->dev, "setting termios - ASPIRE\n"); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1710 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1711 | cflag = tty->termios.c_cflag; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1712 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1713 | dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__, |
Linus Torvalds | d9a8074 | 2012-10-01 13:23:01 -0700 | [diff] [blame] | 1714 | tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag)); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1715 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1716 | dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__, |
| 1717 | old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1718 | |
| 1719 | /* change the port settings to the new ones specified */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1720 | change_port_settings(tty, mos7720_port, old_termios); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1721 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1722 | if (port->read_urb->status != -EINPROGRESS) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1723 | status = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 1724 | if (status) |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1725 | dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1726 | } |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * get_lsr_info - get line status register info |
| 1731 | * |
| 1732 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 1733 | * is emptied. On bus types like RS485, the transmitter must |
| 1734 | * release the bus after transmitting. This must be done when |
| 1735 | * the transmit shift register is empty, not be done when the |
| 1736 | * transmit holding register is empty. This functionality |
| 1737 | * allows an RS485 driver to be written in user space. |
| 1738 | */ |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1739 | static int get_lsr_info(struct tty_struct *tty, |
| 1740 | struct moschip_port *mos7720_port, unsigned int __user *value) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1741 | { |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1742 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1743 | unsigned int result = 0; |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1744 | unsigned char data = 0; |
| 1745 | int port_number = port->number - port->serial->minor; |
| 1746 | int count; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1747 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1748 | count = mos7720_chars_in_buffer(tty); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1749 | if (count == 0) { |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1750 | read_mos_reg(port->serial, port_number, LSR, &data); |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1751 | if ((data & (UART_LSR_TEMT | UART_LSR_THRE)) |
| 1752 | == (UART_LSR_TEMT | UART_LSR_THRE)) { |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1753 | dev_dbg(&port->dev, "%s -- Empty\n", __func__); |
Kees Schoenmakers | 2f9ea55 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1754 | result = TIOCSER_TEMT; |
| 1755 | } |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1756 | } |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1757 | if (copy_to_user(value, &result, sizeof(int))) |
| 1758 | return -EFAULT; |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 1762 | static int mos7720_tiocmget(struct tty_struct *tty) |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1763 | { |
| 1764 | struct usb_serial_port *port = tty->driver_data; |
| 1765 | struct moschip_port *mos7720_port = usb_get_serial_port_data(port); |
| 1766 | unsigned int result = 0; |
| 1767 | unsigned int mcr ; |
| 1768 | unsigned int msr ; |
| 1769 | |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1770 | mcr = mos7720_port->shadowMCR; |
| 1771 | msr = mos7720_port->shadowMSR; |
| 1772 | |
| 1773 | result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */ |
| 1774 | | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */ |
| 1775 | | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */ |
| 1776 | | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */ |
| 1777 | | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */ |
| 1778 | | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */ |
| 1779 | |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1780 | return result; |
| 1781 | } |
| 1782 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 1783 | static int mos7720_tiocmset(struct tty_struct *tty, |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1784 | unsigned int set, unsigned int clear) |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1785 | { |
| 1786 | struct usb_serial_port *port = tty->driver_data; |
| 1787 | struct moschip_port *mos7720_port = usb_get_serial_port_data(port); |
| 1788 | unsigned int mcr ; |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1789 | |
| 1790 | mcr = mos7720_port->shadowMCR; |
| 1791 | |
| 1792 | if (set & TIOCM_RTS) |
| 1793 | mcr |= UART_MCR_RTS; |
| 1794 | if (set & TIOCM_DTR) |
| 1795 | mcr |= UART_MCR_DTR; |
| 1796 | if (set & TIOCM_LOOP) |
| 1797 | mcr |= UART_MCR_LOOP; |
| 1798 | |
| 1799 | if (clear & TIOCM_RTS) |
| 1800 | mcr &= ~UART_MCR_RTS; |
| 1801 | if (clear & TIOCM_DTR) |
| 1802 | mcr &= ~UART_MCR_DTR; |
| 1803 | if (clear & TIOCM_LOOP) |
| 1804 | mcr &= ~UART_MCR_LOOP; |
| 1805 | |
| 1806 | mos7720_port->shadowMCR = mcr; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1807 | write_mos_reg(port->serial, port->number - port->serial->minor, |
| 1808 | MCR, mos7720_port->shadowMCR); |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 1813 | static int mos7720_get_icount(struct tty_struct *tty, |
| 1814 | struct serial_icounter_struct *icount) |
| 1815 | { |
| 1816 | struct usb_serial_port *port = tty->driver_data; |
| 1817 | struct moschip_port *mos7720_port; |
| 1818 | struct async_icount cnow; |
| 1819 | |
| 1820 | mos7720_port = usb_get_serial_port_data(port); |
| 1821 | cnow = mos7720_port->icount; |
| 1822 | |
| 1823 | icount->cts = cnow.cts; |
| 1824 | icount->dsr = cnow.dsr; |
| 1825 | icount->rng = cnow.rng; |
| 1826 | icount->dcd = cnow.dcd; |
| 1827 | icount->rx = cnow.rx; |
| 1828 | icount->tx = cnow.tx; |
| 1829 | icount->frame = cnow.frame; |
| 1830 | icount->overrun = cnow.overrun; |
| 1831 | icount->parity = cnow.parity; |
| 1832 | icount->brk = cnow.brk; |
| 1833 | icount->buf_overrun = cnow.buf_overrun; |
| 1834 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1835 | dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__, |
| 1836 | icount->rx, icount->tx); |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 1837 | return 0; |
| 1838 | } |
| 1839 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1840 | static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd, |
| 1841 | unsigned int __user *value) |
| 1842 | { |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 1843 | unsigned int mcr; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1844 | unsigned int arg; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1845 | |
| 1846 | struct usb_serial_port *port; |
| 1847 | |
| 1848 | if (mos7720_port == NULL) |
| 1849 | return -1; |
| 1850 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1851 | port = (struct usb_serial_port *)mos7720_port->port; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1852 | mcr = mos7720_port->shadowMCR; |
| 1853 | |
| 1854 | if (copy_from_user(&arg, value, sizeof(int))) |
| 1855 | return -EFAULT; |
| 1856 | |
| 1857 | switch (cmd) { |
| 1858 | case TIOCMBIS: |
| 1859 | if (arg & TIOCM_RTS) |
| 1860 | mcr |= UART_MCR_RTS; |
| 1861 | if (arg & TIOCM_DTR) |
| 1862 | mcr |= UART_MCR_RTS; |
| 1863 | if (arg & TIOCM_LOOP) |
| 1864 | mcr |= UART_MCR_LOOP; |
| 1865 | break; |
| 1866 | |
| 1867 | case TIOCMBIC: |
| 1868 | if (arg & TIOCM_RTS) |
| 1869 | mcr &= ~UART_MCR_RTS; |
| 1870 | if (arg & TIOCM_DTR) |
| 1871 | mcr &= ~UART_MCR_RTS; |
| 1872 | if (arg & TIOCM_LOOP) |
| 1873 | mcr &= ~UART_MCR_LOOP; |
| 1874 | break; |
| 1875 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | mos7720_port->shadowMCR = mcr; |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 1879 | write_mos_reg(port->serial, port->number - port->serial->minor, |
| 1880 | MCR, mos7720_port->shadowMCR); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1885 | static int get_serial_info(struct moschip_port *mos7720_port, |
| 1886 | struct serial_struct __user *retinfo) |
| 1887 | { |
| 1888 | struct serial_struct tmp; |
| 1889 | |
| 1890 | if (!retinfo) |
| 1891 | return -EFAULT; |
| 1892 | |
| 1893 | memset(&tmp, 0, sizeof(tmp)); |
| 1894 | |
| 1895 | tmp.type = PORT_16550A; |
| 1896 | tmp.line = mos7720_port->port->serial->minor; |
| 1897 | tmp.port = mos7720_port->port->number; |
| 1898 | tmp.irq = 0; |
| 1899 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1900 | tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1901 | tmp.baud_base = 9600; |
| 1902 | tmp.close_delay = 5*HZ; |
| 1903 | tmp.closing_wait = 30*HZ; |
| 1904 | |
| 1905 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 1906 | return -EFAULT; |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 1910 | static int mos7720_ioctl(struct tty_struct *tty, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1911 | unsigned int cmd, unsigned long arg) |
| 1912 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1913 | struct usb_serial_port *port = tty->driver_data; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1914 | struct moschip_port *mos7720_port; |
| 1915 | struct async_icount cnow; |
| 1916 | struct async_icount cprev; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1917 | |
| 1918 | mos7720_port = usb_get_serial_port_data(port); |
| 1919 | if (mos7720_port == NULL) |
| 1920 | return -ENODEV; |
| 1921 | |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1922 | dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1923 | |
| 1924 | switch (cmd) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1925 | case TIOCSERGETLSR: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1926 | dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1927 | return get_lsr_info(tty, mos7720_port, |
| 1928 | (unsigned int __user *)arg); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1929 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 1930 | /* FIXME: These should be using the mode methods */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1931 | case TIOCMBIS: |
| 1932 | case TIOCMBIC: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1933 | dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1934 | return set_modem_info(mos7720_port, cmd, |
| 1935 | (unsigned int __user *)arg); |
| 1936 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1937 | case TIOCGSERIAL: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1938 | dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1939 | return get_serial_info(mos7720_port, |
| 1940 | (struct serial_struct __user *)arg); |
| 1941 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1942 | case TIOCMIWAIT: |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 1943 | dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1944 | cprev = mos7720_port->icount; |
| 1945 | while (1) { |
| 1946 | if (signal_pending(current)) |
| 1947 | return -ERESTARTSYS; |
| 1948 | cnow = mos7720_port->icount; |
| 1949 | if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && |
| 1950 | cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) |
| 1951 | return -EIO; /* no change => error */ |
| 1952 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 1953 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 1954 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 1955 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1956 | return 0; |
| 1957 | } |
| 1958 | cprev = cnow; |
| 1959 | } |
| 1960 | /* NOTREACHED */ |
| 1961 | break; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | return -ENOIOCTLCMD; |
| 1965 | } |
| 1966 | |
| 1967 | static int mos7720_startup(struct usb_serial *serial) |
| 1968 | { |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1969 | struct usb_device *dev; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1970 | char data; |
Huzaifa Sidhpurwala | 91f58ae | 2011-02-21 12:58:45 +0530 | [diff] [blame] | 1971 | u16 product; |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 1972 | int ret_val; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1973 | |
Huzaifa Sidhpurwala | 91f58ae | 2011-02-21 12:58:45 +0530 | [diff] [blame] | 1974 | product = le16_to_cpu(serial->dev->descriptor.idProduct); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 1975 | dev = serial->dev; |
| 1976 | |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 1977 | /* |
| 1978 | * The 7715 uses the first bulk in/out endpoint pair for the parallel |
| 1979 | * port, and the second for the serial port. Because the usbserial core |
| 1980 | * assumes both pairs are serial ports, we must engage in a bit of |
| 1981 | * subterfuge and swap the pointers for ports 0 and 1 in order to make |
| 1982 | * port 0 point to the serial port. However, both moschip devices use a |
| 1983 | * single interrupt-in endpoint for both ports (as mentioned a little |
| 1984 | * further down), and this endpoint was assigned to port 0. So after |
| 1985 | * the swap, we must copy the interrupt endpoint elements from port 1 |
| 1986 | * (as newly assigned) to port 0, and null out port 1 pointers. |
| 1987 | */ |
| 1988 | if (product == MOSCHIP_DEVICE_ID_7715) { |
| 1989 | struct usb_serial_port *tmp = serial->port[0]; |
| 1990 | serial->port[0] = serial->port[1]; |
| 1991 | serial->port[1] = tmp; |
| 1992 | serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb; |
| 1993 | serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer; |
| 1994 | serial->port[0]->interrupt_in_endpointAddress = |
| 1995 | tmp->interrupt_in_endpointAddress; |
| 1996 | serial->port[1]->interrupt_in_urb = NULL; |
| 1997 | serial->port[1]->interrupt_in_buffer = NULL; |
| 1998 | } |
| 1999 | |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2000 | /* setting configuration feature to one */ |
| 2001 | usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 2002 | (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2003 | |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 2004 | /* start the interrupt urb */ |
| 2005 | ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL); |
| 2006 | if (ret_val) |
| 2007 | dev_err(&dev->dev, |
| 2008 | "%s - Error %d submitting control urb\n", |
| 2009 | __func__, ret_val); |
| 2010 | |
| 2011 | #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT |
| 2012 | if (product == MOSCHIP_DEVICE_ID_7715) { |
| 2013 | ret_val = mos7715_parport_init(serial); |
| 2014 | if (ret_val < 0) |
| 2015 | return ret_val; |
| 2016 | } |
| 2017 | #endif |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 2018 | /* LSR For Port 1 */ |
Mike Dunn | 63b9176 | 2010-04-15 17:02:09 -0400 | [diff] [blame] | 2019 | read_mos_reg(serial, 0, LSR, &data); |
Greg Kroah-Hartman | 9eecf80 | 2012-09-14 15:08:33 -0700 | [diff] [blame] | 2020 | dev_dbg(&dev->dev, "LSR:%x\n", data); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2021 | |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 2025 | static void mos7720_release(struct usb_serial *serial) |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2026 | { |
Mike Dunn | b69578d | 2010-04-15 17:01:33 -0400 | [diff] [blame] | 2027 | #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT |
| 2028 | /* close the parallel port */ |
| 2029 | |
| 2030 | if (le16_to_cpu(serial->dev->descriptor.idProduct) |
| 2031 | == MOSCHIP_DEVICE_ID_7715) { |
| 2032 | struct urbtracker *urbtrack; |
| 2033 | unsigned long flags; |
| 2034 | struct mos7715_parport *mos_parport = |
| 2035 | usb_get_serial_data(serial); |
| 2036 | |
| 2037 | /* prevent NULL ptr dereference in port callbacks */ |
| 2038 | spin_lock(&release_lock); |
| 2039 | mos_parport->pp->private_data = NULL; |
| 2040 | spin_unlock(&release_lock); |
| 2041 | |
| 2042 | /* wait for synchronous usb calls to return */ |
| 2043 | if (mos_parport->msg_pending) |
| 2044 | wait_for_completion_timeout(&mos_parport->syncmsg_compl, |
| 2045 | MOS_WDR_TIMEOUT); |
| 2046 | |
| 2047 | parport_remove_port(mos_parport->pp); |
| 2048 | usb_set_serial_data(serial, NULL); |
| 2049 | mos_parport->serial = NULL; |
| 2050 | |
| 2051 | /* if tasklet currently scheduled, wait for it to complete */ |
| 2052 | tasklet_kill(&mos_parport->urb_tasklet); |
| 2053 | |
| 2054 | /* unlink any urbs sent by the tasklet */ |
| 2055 | spin_lock_irqsave(&mos_parport->listlock, flags); |
| 2056 | list_for_each_entry(urbtrack, |
| 2057 | &mos_parport->active_urbs, |
| 2058 | urblist_entry) |
| 2059 | usb_unlink_urb(urbtrack->urb); |
| 2060 | spin_unlock_irqrestore(&mos_parport->listlock, flags); |
| 2061 | |
| 2062 | kref_put(&mos_parport->ref_count, destroy_mos_parport); |
| 2063 | } |
| 2064 | #endif |
Johan Hovold | 4230af5 | 2012-10-25 10:29:05 +0200 | [diff] [blame^] | 2065 | } |
| 2066 | |
| 2067 | static int mos7720_port_probe(struct usb_serial_port *port) |
| 2068 | { |
| 2069 | struct moschip_port *mos7720_port; |
| 2070 | |
| 2071 | mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL); |
| 2072 | if (!mos7720_port) |
| 2073 | return -ENOMEM; |
| 2074 | |
| 2075 | /* Initialize all port interrupt end point to port 0 int endpoint. |
| 2076 | * Our device has only one interrupt endpoint common to all ports. |
| 2077 | */ |
| 2078 | port->interrupt_in_endpointAddress = |
| 2079 | port->serial->port[0]->interrupt_in_endpointAddress; |
| 2080 | mos7720_port->port = port; |
| 2081 | |
| 2082 | usb_set_serial_port_data(port, mos7720_port); |
| 2083 | |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | static int mos7720_port_remove(struct usb_serial_port *port) |
| 2088 | { |
| 2089 | struct moschip_port *mos7720_port; |
| 2090 | |
| 2091 | mos7720_port = usb_get_serial_port_data(port); |
| 2092 | kfree(mos7720_port); |
| 2093 | |
| 2094 | return 0; |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | static struct usb_serial_driver moschip7720_2port_driver = { |
| 2098 | .driver = { |
| 2099 | .owner = THIS_MODULE, |
| 2100 | .name = "moschip7720", |
| 2101 | }, |
| 2102 | .description = "Moschip 2 port adapter", |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 2103 | .id_table = id_table, |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 2104 | .calc_num_ports = mos77xx_calc_num_ports, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2105 | .open = mos7720_open, |
| 2106 | .close = mos7720_close, |
| 2107 | .throttle = mos7720_throttle, |
| 2108 | .unthrottle = mos7720_unthrottle, |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 2109 | .probe = mos77xx_probe, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2110 | .attach = mos7720_startup, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 2111 | .release = mos7720_release, |
Johan Hovold | 4230af5 | 2012-10-25 10:29:05 +0200 | [diff] [blame^] | 2112 | .port_probe = mos7720_port_probe, |
| 2113 | .port_remove = mos7720_port_remove, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2114 | .ioctl = mos7720_ioctl, |
Kees Schoenmakers | 0f608f8 | 2009-09-19 13:13:18 -0700 | [diff] [blame] | 2115 | .tiocmget = mos7720_tiocmget, |
| 2116 | .tiocmset = mos7720_tiocmset, |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 2117 | .get_icount = mos7720_get_icount, |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2118 | .set_termios = mos7720_set_termios, |
| 2119 | .write = mos7720_write, |
| 2120 | .write_room = mos7720_write_room, |
| 2121 | .chars_in_buffer = mos7720_chars_in_buffer, |
| 2122 | .break_ctl = mos7720_break, |
| 2123 | .read_bulk_callback = mos7720_bulk_in_callback, |
Mike Dunn | fb088e3 | 2010-01-26 12:12:12 -0500 | [diff] [blame] | 2124 | .read_int_callback = NULL /* dynamically assigned in probe() */ |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2125 | }; |
| 2126 | |
Alan Stern | 4d2a7af | 2012-02-23 14:57:09 -0500 | [diff] [blame] | 2127 | static struct usb_serial_driver * const serial_drivers[] = { |
| 2128 | &moschip7720_2port_driver, NULL |
| 2129 | }; |
| 2130 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 2131 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2132 | |
Alan Cox | 4da1a17 | 2008-07-22 11:16:21 +0100 | [diff] [blame] | 2133 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2134 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 0f64478 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 2135 | MODULE_LICENSE("GPL"); |