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