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