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