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