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