blob: e0aa031c54186f07d71830eb372bc2052ba87c14 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
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-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
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 Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070037
38
39/*
40 * Version Information
41 */
42#define DRIVER_VERSION "1.0.0.4F"
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
49#define MOS_PORT1 0x0200
50#define MOS_PORT2 0x0300
51#define MOS_VENREG 0x0000
52#define MOS_MAX_PORT 0x02
53#define MOS_WRITE 0x0E
54#define MOS_READ 0x0D
55
56/* Interrupt Rotinue Defines */
57#define SERIAL_IIR_RLS 0x06
58#define SERIAL_IIR_RDA 0x04
59#define SERIAL_IIR_CTI 0x0c
60#define SERIAL_IIR_THR 0x02
61#define SERIAL_IIR_MS 0x00
62
63#define NUM_URBS 16 /* URB Count */
64#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
65
66/* This structure holds all of the local port information */
Alan Cox4da1a172008-07-22 11:16:21 +010067struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070068 __u8 shadowLCR; /* last LCR value received */
69 __u8 shadowMCR; /* last MCR value received */
70 __u8 shadowMSR; /* last MSR value received */
71 char open;
72 struct async_icount icount;
73 struct usb_serial_port *port; /* loop back to the owner */
74 struct urb *write_urb_pool[NUM_URBS];
75};
76
77/* This structure holds all of the individual serial device information */
Alan Cox4da1a172008-07-22 11:16:21 +010078struct moschip_serial {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070079 int interrupt_started;
80};
81
82static int debug;
83
Mike Dunnfb088e32010-01-26 12:12:12 -050084static struct usb_serial_driver moschip7720_2port_driver;
85
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070086#define USB_VENDOR_ID_MOSCHIP 0x9710
87#define MOSCHIP_DEVICE_ID_7720 0x7720
88#define MOSCHIP_DEVICE_ID_7715 0x7715
89
Németh Márton7d40d7e2010-01-10 15:34:24 +010090static const struct usb_device_id moschip_port_id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010091 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050092 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070093 { } /* terminating entry */
94};
95MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
96
97
98/*
99 * mos7720_interrupt_callback
100 * this is the callback function for when we have received data on the
101 * interrupt endpoint.
102 */
103static void mos7720_interrupt_callback(struct urb *urb)
104{
105 int result;
106 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700107 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100108 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700109 __u8 sp1;
110 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700111
Alan Cox4da1a172008-07-22 11:16:21 +0100112 dbg("%s", " : Entering\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700113
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700114 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700115 case 0:
116 /* success */
117 break;
118 case -ECONNRESET:
119 case -ENOENT:
120 case -ESHUTDOWN:
121 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800122 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700123 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700124 return;
125 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800126 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700127 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700128 goto exit;
129 }
130
131 length = urb->actual_length;
132 data = urb->transfer_buffer;
133
134 /* Moschip get 4 bytes
135 * Byte 1 IIR Port 1 (port.number is 0)
136 * Byte 2 IIR Port 2 (port.number is 1)
137 * Byte 3 --------------
138 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100139
140 /* the above description is inverted
141 * oneukum 2007-03-14 */
142
143 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700144 dbg("Wrong data !!!");
145 return;
146 }
147
Oliver Neukum325b70c2007-03-19 13:58:29 +0100148 sp1 = data[3];
149 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700150
Oliver Neukum325b70c2007-03-19 13:58:29 +0100151 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700152 /* No Interrupt Pending in both the ports */
153 dbg("No Interrupt !!!");
154 } else {
155 switch (sp1 & 0x0f) {
156 case SERIAL_IIR_RLS:
157 dbg("Serial Port 1: Receiver status error or address "
158 "bit detected in 9-bit mode\n");
159 break;
160 case SERIAL_IIR_CTI:
161 dbg("Serial Port 1: Receiver time out");
162 break;
163 case SERIAL_IIR_MS:
164 dbg("Serial Port 1: Modem status change");
165 break;
166 }
167
168 switch (sp2 & 0x0f) {
169 case SERIAL_IIR_RLS:
170 dbg("Serial Port 2: Receiver status error or address "
171 "bit detected in 9-bit mode");
172 break;
173 case SERIAL_IIR_CTI:
174 dbg("Serial Port 2: Receiver time out");
175 break;
176 case SERIAL_IIR_MS:
177 dbg("Serial Port 2: Modem status change");
178 break;
179 }
180 }
181
182exit:
183 result = usb_submit_urb(urb, GFP_ATOMIC);
184 if (result)
185 dev_err(&urb->dev->dev,
186 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800187 __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700188 return;
189}
190
191/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500192 * mos7715_interrupt_callback
193 * this is the 7715's callback function for when we have received data on
194 * the interrupt endpoint.
195 */
196static void mos7715_interrupt_callback(struct urb *urb)
197{
198 int result;
199 int length;
200 int status = urb->status;
201 __u8 *data;
202 __u8 iir;
203
204 switch (status) {
205 case 0:
206 /* success */
207 break;
208 case -ECONNRESET:
209 case -ENOENT:
210 case -ESHUTDOWN:
211 /* this urb is terminated, clean up */
212 dbg("%s - urb shutting down with status: %d", __func__,
213 status);
214 return;
215 default:
216 dbg("%s - nonzero urb status received: %d", __func__,
217 status);
218 goto exit;
219 }
220
221 length = urb->actual_length;
222 data = urb->transfer_buffer;
223
224 /* Structure of data from 7715 device:
225 * Byte 1: IIR serial Port
226 * Byte 2: unused
227 * Byte 2: DSR parallel port
228 * Byte 4: FIFO status for both */
229
230 if (unlikely(length != 4)) {
231 dbg("Wrong data !!!");
232 return;
233 }
234
235 iir = data[0];
236 if (!(iir & 0x01)) { /* serial port interrupt pending */
237 switch (iir & 0x0f) {
238 case SERIAL_IIR_RLS:
239 dbg("Serial Port: Receiver status error or address "
240 "bit detected in 9-bit mode\n");
241 break;
242 case SERIAL_IIR_CTI:
243 dbg("Serial Port: Receiver time out");
244 break;
245 case SERIAL_IIR_MS:
246 dbg("Serial Port: Modem status change");
247 break;
248 }
249 }
250
251exit:
252 result = usb_submit_urb(urb, GFP_ATOMIC);
253 if (result)
254 dev_err(&urb->dev->dev,
255 "%s - Error %d submitting control urb\n",
256 __func__, result);
257 return;
258}
259
260/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700261 * mos7720_bulk_in_callback
262 * this is the callback function for when we have received data on the
263 * bulk in endpoint.
264 */
265static void mos7720_bulk_in_callback(struct urb *urb)
266{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700267 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700268 unsigned char *data ;
269 struct usb_serial_port *port;
270 struct moschip_port *mos7720_port;
271 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700272 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700273
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700274 if (status) {
275 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700276 return;
277 }
278
279 mos7720_port = urb->context;
280 if (!mos7720_port) {
Alan Cox4da1a172008-07-22 11:16:21 +0100281 dbg("%s", "NULL mos7720_port pointer \n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700282 return ;
283 }
284
285 port = mos7720_port->port;
286
Harvey Harrison441b62c2008-03-03 16:08:34 -0800287 dbg("Entering...%s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700288
289 data = urb->transfer_buffer;
290
Alan Cox4a90f092008-10-13 10:39:46 +0100291 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700292 if (tty && urb->actual_length) {
293 tty_buffer_request_room(tty, urb->actual_length);
294 tty_insert_flip_string(tty, data, urb->actual_length);
295 tty_flip_buffer_push(tty);
296 }
Alan Cox4a90f092008-10-13 10:39:46 +0100297 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700298
299 if (!port->read_urb) {
300 dbg("URB KILLED !!!");
301 return;
302 }
303
304 if (port->read_urb->status != -EINPROGRESS) {
305 port->read_urb->dev = port->serial->dev;
306
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700307 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
308 if (retval)
309 dbg("usb_submit_urb(read bulk) failed, retval = %d",
310 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700311 }
312}
313
314/*
315 * mos7720_bulk_out_data_callback
316 * this is the callback function for when we have finished sending serial
317 * data on the bulk out endpoint.
318 */
319static void mos7720_bulk_out_data_callback(struct urb *urb)
320{
321 struct moschip_port *mos7720_port;
322 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700323 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700324
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700325 if (status) {
326 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700327 return;
328 }
329
330 mos7720_port = urb->context;
331 if (!mos7720_port) {
332 dbg("NULL mos7720_port pointer");
333 return ;
334 }
335
336 dbg("Entering .........");
337
Alan Cox4a90f092008-10-13 10:39:46 +0100338 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700339
Jiri Slabyb963a842007-02-10 01:44:55 -0800340 if (tty && mos7720_port->open)
341 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100342 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700343}
344
345/*
346 * send_mos_cmd
347 * this function will be used for sending command to device
348 */
349static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
Johan Hovoldeb771e22009-12-28 23:01:54 +0100350 __u16 index, u8 *data)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700351{
352 int status;
Johan Hovoldeb771e22009-12-28 23:01:54 +0100353 u8 *buf;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700354 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700355
356 if (value < MOS_MAX_PORT) {
Alan Cox4da1a172008-07-22 11:16:21 +0100357 if (product == MOSCHIP_DEVICE_ID_7715)
Mike Dunnfb088e32010-01-26 12:12:12 -0500358 value = 0x0200; /* identifies the 7715's serial port */
Alan Cox4da1a172008-07-22 11:16:21 +0100359 else
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700360 value = value*0x100+0x200;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700361 } else {
362 value = 0x0000;
363 if ((product == MOSCHIP_DEVICE_ID_7715) &&
364 (index != 0x08)) {
365 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
Alan Cox4da1a172008-07-22 11:16:21 +0100366 /* index = 0x01 ; */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700367 }
368 }
369
370 if (request == MOS_WRITE) {
Johan Hovoldeb771e22009-12-28 23:01:54 +0100371 value = value + *data;
372 status = usb_control_msg(serial->dev,
373 usb_sndctrlpipe(serial->dev, 0), MOS_WRITE,
374 0x40, value, index, NULL, 0, MOS_WDR_TIMEOUT);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700375 } else {
Johan Hovoldeb771e22009-12-28 23:01:54 +0100376 buf = kmalloc(1, GFP_KERNEL);
377 if (!buf) {
378 status = -ENOMEM;
379 goto out;
380 }
381 status = usb_control_msg(serial->dev,
382 usb_rcvctrlpipe(serial->dev, 0), MOS_READ,
383 0xc0, value, index, buf, 1, MOS_WDR_TIMEOUT);
384 *data = *buf;
385 kfree(buf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700386 }
Johan Hovoldeb771e22009-12-28 23:01:54 +0100387out:
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700388 if (status < 0)
Alan Cox4da1a172008-07-22 11:16:21 +0100389 dbg("Command Write failed Value %x index %x\n", value, index);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700390
391 return status;
392}
393
Mike Dunnfb088e32010-01-26 12:12:12 -0500394
395/*
396 * mos77xx_probe
397 * this function installs the appropriate read interrupt endpoint callback
398 * depending on whether the device is a 7720 or 7715, thus avoiding costly
399 * run-time checks in the high-frequency callback routine itself.
400 */
401static int mos77xx_probe(struct usb_serial *serial,
402 const struct usb_device_id *id)
403{
404 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
405 moschip7720_2port_driver.read_int_callback =
406 mos7715_interrupt_callback;
407 else
408 moschip7720_2port_driver.read_int_callback =
409 mos7720_interrupt_callback;
410
411 return 0;
412}
413
414static int mos77xx_calc_num_ports(struct usb_serial *serial)
415{
416 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
417 if (product == MOSCHIP_DEVICE_ID_7715)
418 return 1;
419
420 return 2;
421}
422
Alan Coxa509a7e2009-09-19 13:13:26 -0700423static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700424{
425 struct usb_serial *serial;
426 struct usb_serial_port *port0;
427 struct urb *urb;
428 struct moschip_serial *mos7720_serial;
429 struct moschip_port *mos7720_port;
430 int response;
431 int port_number;
432 char data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100433 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700434 int j;
435
436 serial = port->serial;
437
438 mos7720_port = usb_get_serial_port_data(port);
439 if (mos7720_port == NULL)
440 return -ENODEV;
441
442 port0 = serial->port[0];
443
444 mos7720_serial = usb_get_serial_data(serial);
445
446 if (mos7720_serial == NULL || port0 == NULL)
447 return -ENODEV;
448
449 usb_clear_halt(serial->dev, port->write_urb->pipe);
450 usb_clear_halt(serial->dev, port->read_urb->pipe);
451
452 /* Initialising the write urb pool */
453 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +0100454 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700455 mos7720_port->write_urb_pool[j] = urb;
456
457 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700458 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700459 continue;
460 }
461
462 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
463 GFP_KERNEL);
464 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700465 dev_err(&port->dev,
466 "%s-out of memory for urb buffers.\n",
467 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100468 usb_free_urb(mos7720_port->write_urb_pool[j]);
469 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700470 continue;
471 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100472 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700473 }
474
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100475 if (!allocated_urbs)
476 return -ENOMEM;
477
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700478 /* Initialize MCS7720 -- Write Init values to corresponding Registers
479 *
480 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700481 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700482 * 1 : IER
483 * 2 : FCR
484 * 3 : LCR
485 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700486 * 5 : LSR
487 * 6 : MSR
488 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700489 *
490 * 0x08 : SP1/2 Control Reg
491 */
492 port_number = port->number - port->serial->minor;
493 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +0100494 dbg("SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700495
496 dbg("Check:Sending Command ..........");
497
498 data = 0x02;
499 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
500 data = 0x02;
501 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
502
503 data = 0x00;
504 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
505 data = 0x00;
506 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
507
508 data = 0xCF;
509 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
510 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100511 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700512 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
513 data = 0x0b;
Alan Cox4da1a172008-07-22 11:16:21 +0100514 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700515 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
516 data = 0x0b;
517 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
518
519 data = 0x00;
520 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
521 data = 0x00;
522 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
523
524/* data = 0x00;
525 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
526 data = 0x03;
527 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
528 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100529 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
530 port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700531*/
532 data = 0x00;
533 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
534
535 data = data | (port->number - port->serial->minor + 1);
536 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
537
538 data = 0x83;
Alan Cox4da1a172008-07-22 11:16:21 +0100539 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700540 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
541 data = 0x0c;
542 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
543 data = 0x00;
544 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
545 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100546 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700547 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
548 data = 0x0c;
549 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
550 data = 0x0c;
551 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
552
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700553 /* see if we've set up our endpoint info yet *
554 * (can't set it up in mos7720_startup as the *
555 * structures were not set up at that time.) */
556 if (!mos7720_serial->interrupt_started) {
557 dbg("Interrupt buffer NULL !!!");
558
559 /* not set up yet, so do it now */
560 mos7720_serial->interrupt_started = 1;
561
562 dbg("To Submit URB !!!");
563
564 /* set up our interrupt urb */
565 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100566 usb_rcvintpipe(serial->dev,
567 port->interrupt_in_endpointAddress),
568 port0->interrupt_in_buffer,
569 port0->interrupt_in_urb->transfer_buffer_length,
570 mos7720_interrupt_callback, mos7720_port,
571 port0->interrupt_in_urb->interval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700572
573 /* start interrupt read for this mos7720 this interrupt *
Alan Cox4da1a172008-07-22 11:16:21 +0100574 * will continue as long as the mos7720 is connected */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700575 dbg("Submit URB over !!!");
576 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
577 if (response)
578 dev_err(&port->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700579 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700581 }
582
583 /* set up our bulk in urb */
584 usb_fill_bulk_urb(port->read_urb, serial->dev,
585 usb_rcvbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100586 port->bulk_in_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700587 port->bulk_in_buffer,
588 port->read_urb->transfer_buffer_length,
589 mos7720_bulk_in_callback, mos7720_port);
590 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
591 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100592 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
593 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700594
595 /* initialize our icount structure */
596 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
597
598 /* initialize our port settings */
599 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
600
601 /* send a open port command */
602 mos7720_port->open = 1;
603
604 return 0;
605}
606
607/*
608 * mos7720_chars_in_buffer
609 * this function is called by the tty driver when it wants to know how many
610 * bytes of data we currently have outstanding in the port (data that has
611 * been written, but hasn't made it out the port yet)
612 * If successful, we return the number of bytes left to be written in the
613 * system,
614 * Otherwise we return a negative error number.
615 */
Alan Cox95da3102008-07-22 11:09:07 +0100616static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700617{
Alan Cox95da3102008-07-22 11:09:07 +0100618 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700619 int i;
620 int chars = 0;
621 struct moschip_port *mos7720_port;
622
Harvey Harrison441b62c2008-03-03 16:08:34 -0800623 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700624
625 mos7720_port = usb_get_serial_port_data(port);
626 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800627 dbg("%s:leaving ...........", __func__);
Alan Cox23198fd2009-07-20 16:05:27 +0100628 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700629 }
630
631 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100632 if (mos7720_port->write_urb_pool[i] &&
633 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700634 chars += URB_TRANSFER_BUFFER_SIZE;
635 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800636 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700637 return chars;
638}
639
Alan Cox335f8512009-06-11 12:26:29 +0100640static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700641{
642 struct usb_serial *serial;
643 struct moschip_port *mos7720_port;
644 char data;
645 int j;
646
647 dbg("mos7720_close:entering...");
648
649 serial = port->serial;
650
651 mos7720_port = usb_get_serial_port_data(port);
652 if (mos7720_port == NULL)
653 return;
654
655 for (j = 0; j < NUM_URBS; ++j)
656 usb_kill_urb(mos7720_port->write_urb_pool[j]);
657
658 /* Freeing Write URBs */
659 for (j = 0; j < NUM_URBS; ++j) {
660 if (mos7720_port->write_urb_pool[j]) {
661 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
662 usb_free_urb(mos7720_port->write_urb_pool[j]);
663 }
664 }
665
666 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100667 * and interrupt read if they exists, otherwise nop */
668 dbg("Shutdown bulk write");
669 usb_kill_urb(port->write_urb);
670 dbg("Shutdown bulk read");
671 usb_kill_urb(port->read_urb);
672
673 mutex_lock(&serial->disc_mutex);
674 /* these commands must not be issued if the device has
675 * been disconnected */
676 if (!serial->disconnected) {
677 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100678 send_mos_cmd(serial, MOS_WRITE,
679 port->number - port->serial->minor, 0x04, &data);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100680
681 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100682 send_mos_cmd(serial, MOS_WRITE,
683 port->number - port->serial->minor, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700684 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100685 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700686 mos7720_port->open = 0;
687
Harvey Harrison441b62c2008-03-03 16:08:34 -0800688 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700689}
690
Alan Cox95da3102008-07-22 11:09:07 +0100691static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700692{
Alan Cox95da3102008-07-22 11:09:07 +0100693 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +0100694 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700695 struct usb_serial *serial;
696 struct moschip_port *mos7720_port;
697
Harvey Harrison441b62c2008-03-03 16:08:34 -0800698 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700699
700 serial = port->serial;
701
702 mos7720_port = usb_get_serial_port_data(port);
703 if (mos7720_port == NULL)
704 return;
705
706 if (break_state == -1)
707 data = mos7720_port->shadowLCR | UART_LCR_SBC;
708 else
709 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
710
711 mos7720_port->shadowLCR = data;
712 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
713 0x03, &data);
714
715 return;
716}
717
718/*
719 * mos7720_write_room
720 * this function is called by the tty driver when it wants to know how many
721 * bytes of data we can accept for a specific port.
722 * If successful, we return the amount of room that we have for this port
723 * Otherwise we return a negative error number.
724 */
Alan Cox95da3102008-07-22 11:09:07 +0100725static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700726{
Alan Cox95da3102008-07-22 11:09:07 +0100727 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700728 struct moschip_port *mos7720_port;
729 int room = 0;
730 int i;
731
Harvey Harrison441b62c2008-03-03 16:08:34 -0800732 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700733
734 mos7720_port = usb_get_serial_port_data(port);
735 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800736 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700737 return -ENODEV;
738 }
739
Alan Coxa5b6f602008-04-08 17:16:06 +0100740 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700741 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100742 if (mos7720_port->write_urb_pool[i] &&
743 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700744 room += URB_TRANSFER_BUFFER_SIZE;
745 }
746
Harvey Harrison441b62c2008-03-03 16:08:34 -0800747 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748 return room;
749}
750
Alan Cox95da3102008-07-22 11:09:07 +0100751static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
752 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700753{
754 int status;
755 int i;
756 int bytes_sent = 0;
757 int transfer_size;
758
759 struct moschip_port *mos7720_port;
760 struct usb_serial *serial;
761 struct urb *urb;
762 const unsigned char *current_position = data;
763
Harvey Harrison441b62c2008-03-03 16:08:34 -0800764 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700765
766 serial = port->serial;
767
768 mos7720_port = usb_get_serial_port_data(port);
769 if (mos7720_port == NULL) {
770 dbg("mos7720_port is NULL");
771 return -ENODEV;
772 }
773
774 /* try to find a free urb in the list */
775 urb = NULL;
776
777 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100778 if (mos7720_port->write_urb_pool[i] &&
779 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700780 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +0100781 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700782 break;
783 }
784 }
785
786 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800787 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700788 goto exit;
789 }
790
791 if (urb->transfer_buffer == NULL) {
792 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
793 GFP_KERNEL);
794 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700795 dev_err(&port->dev, "%s no more kernel memory...\n",
796 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700797 goto exit;
798 }
799 }
Alan Cox4da1a172008-07-22 11:16:21 +0100800 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700801
802 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800803 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700804 urb->transfer_buffer);
805
806 /* fill urb with data and submit */
807 usb_fill_bulk_urb(urb, serial->dev,
808 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100809 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700810 urb->transfer_buffer, transfer_size,
811 mos7720_bulk_out_data_callback, mos7720_port);
812
813 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +0100814 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700815 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700816 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
817 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700818 bytes_sent = status;
819 goto exit;
820 }
821 bytes_sent = transfer_size;
822
823exit:
824 return bytes_sent;
825}
826
Alan Cox95da3102008-07-22 11:09:07 +0100827static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700828{
Alan Cox95da3102008-07-22 11:09:07 +0100829 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700830 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700831 int status;
832
Harvey Harrison441b62c2008-03-03 16:08:34 -0800833 dbg("%s- port %d\n", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700834
835 mos7720_port = usb_get_serial_port_data(port);
836
837 if (mos7720_port == NULL)
838 return;
839
840 if (!mos7720_port->open) {
841 dbg("port not opened");
842 return;
843 }
844
Harvey Harrison441b62c2008-03-03 16:08:34 -0800845 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700846
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700847 /* if we are implementing XON/XOFF, send the stop character */
848 if (I_IXOFF(tty)) {
849 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100850 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700851 if (status <= 0)
852 return;
853 }
854
855 /* if we are implementing RTS/CTS, toggle that line */
856 if (tty->termios->c_cflag & CRTSCTS) {
857 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
858 status = send_mos_cmd(port->serial, MOS_WRITE,
859 port->number - port->serial->minor,
860 UART_MCR, &mos7720_port->shadowMCR);
861 if (status != 0)
862 return;
863 }
864}
865
Alan Cox95da3102008-07-22 11:09:07 +0100866static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700867{
Alan Cox95da3102008-07-22 11:09:07 +0100868 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700869 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100870 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700871
872 if (mos7720_port == NULL)
873 return;
874
875 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700877 return;
878 }
879
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700881
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700882 /* if we are implementing XON/XOFF, send the start character */
883 if (I_IXOFF(tty)) {
884 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100885 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700886 if (status <= 0)
887 return;
888 }
889
890 /* if we are implementing RTS/CTS, toggle that line */
891 if (tty->termios->c_cflag & CRTSCTS) {
892 mos7720_port->shadowMCR |= UART_MCR_RTS;
893 status = send_mos_cmd(port->serial, MOS_WRITE,
894 port->number - port->serial->minor,
895 UART_MCR, &mos7720_port->shadowMCR);
896 if (status != 0)
897 return;
898 }
899}
900
901static int set_higher_rates(struct moschip_port *mos7720_port,
902 unsigned int baud)
903{
904 unsigned char data;
905 struct usb_serial_port *port;
906 struct usb_serial *serial;
907 int port_number;
908
909 if (mos7720_port == NULL)
910 return -EINVAL;
911
912 port = mos7720_port->port;
913 serial = port->serial;
914
Alan Cox4da1a172008-07-22 11:16:21 +0100915 /***********************************************
916 * Init Sequence for higher rates
917 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700918 dbg("Sending Setting Commands ..........");
919 port_number = port->number - port->serial->minor;
920
921 data = 0x000;
922 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
923 data = 0x000;
924 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
925 data = 0x0CF;
926 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
927 data = 0x00b;
Alan Cox4da1a172008-07-22 11:16:21 +0100928 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700929 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
930 data = 0x00b;
931 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
932
933 data = 0x000;
934 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
935 data = 0x000;
936 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
937
938
Alan Cox4da1a172008-07-22 11:16:21 +0100939 /***********************************************
940 * Set for higher rates *
941 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700942
943 data = baud * 0x10;
Alan Cox4da1a172008-07-22 11:16:21 +0100944 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700945
946 data = 0x003;
947 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
948 data = 0x003;
949 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
950
951 data = 0x02b;
Alan Cox4da1a172008-07-22 11:16:21 +0100952 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
954 data = 0x02b;
955 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
956
Alan Cox4da1a172008-07-22 11:16:21 +0100957 /***********************************************
958 * Set DLL/DLM
959 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700960
961 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100962 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700963 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
964
965 data = 0x001; /* DLL */
Alan Cox4da1a172008-07-22 11:16:21 +0100966 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700967 data = 0x000; /* DLM */
Alan Cox4da1a172008-07-22 11:16:21 +0100968 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700969
970 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100971 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700972 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
973
974 return 0;
975}
976
977/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +0100978struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700979 __u32 baudrate;
980 __u16 divisor;
981};
982
983/* Define table of divisors for moschip 7720 hardware *
984 * These assume a 3.6864MHz crystal, the standard /16, and *
985 * MCR.7 = 0. */
986static struct divisor_table_entry divisor_table[] = {
987 { 50, 2304},
988 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
989 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
990 { 150, 768},
991 { 300, 384},
992 { 600, 192},
993 { 1200, 96},
994 { 1800, 64},
995 { 2400, 48},
996 { 4800, 24},
997 { 7200, 16},
998 { 9600, 12},
999 { 19200, 6},
1000 { 38400, 3},
1001 { 57600, 2},
1002 { 115200, 1},
1003};
1004
1005/*****************************************************************************
1006 * calc_baud_rate_divisor
1007 * this function calculates the proper baud rate divisor for the specified
1008 * baud rate.
1009 *****************************************************************************/
1010static int calc_baud_rate_divisor(int baudrate, int *divisor)
1011{
1012 int i;
1013 __u16 custom;
1014 __u16 round1;
1015 __u16 round;
1016
1017
Harvey Harrison441b62c2008-03-03 16:08:34 -08001018 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001019
1020 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1021 if (divisor_table[i].baudrate == baudrate) {
1022 *divisor = divisor_table[i].divisor;
1023 return 0;
1024 }
1025 }
1026
Alan Cox4da1a172008-07-22 11:16:21 +01001027 /* After trying for all the standard baud rates *
1028 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029 if (baudrate > 75 && baudrate < 230400) {
1030 /* get the divisor */
1031 custom = (__u16)(230400L / baudrate);
1032
1033 /* Check for round off */
1034 round1 = (__u16)(2304000L / baudrate);
1035 round = (__u16)(round1 - (custom * 10));
1036 if (round > 4)
1037 custom++;
1038 *divisor = custom;
1039
Alan Cox4da1a172008-07-22 11:16:21 +01001040 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001041 return 0;
1042 }
1043
1044 dbg("Baud calculation Failed...");
1045 return -EINVAL;
1046}
1047
1048/*
1049 * send_cmd_write_baud_rate
1050 * this function sends the proper command to change the baud rate of the
1051 * specified port.
1052 */
1053static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1054 int baudrate)
1055{
1056 struct usb_serial_port *port;
1057 struct usb_serial *serial;
1058 int divisor;
1059 int status;
1060 unsigned char data;
1061 unsigned char number;
1062
1063 if (mos7720_port == NULL)
1064 return -1;
1065
1066 port = mos7720_port->port;
1067 serial = port->serial;
1068
Harvey Harrison441b62c2008-03-03 16:08:34 -08001069 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001070
1071 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073
Alan Cox4da1a172008-07-22 11:16:21 +01001074 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001075 status = calc_baud_rate_divisor(baudrate, &divisor);
1076 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001077 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001078 return status;
1079 }
1080
Alan Cox4da1a172008-07-22 11:16:21 +01001081 /* Enable access to divisor latch */
1082 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
1083 mos7720_port->shadowLCR = data;
1084 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001085
1086 /* Write the divisor */
1087 data = ((unsigned char)(divisor & 0xff));
Alan Cox4da1a172008-07-22 11:16:21 +01001088 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001089
1090 data = ((unsigned char)((divisor & 0xff00) >> 8));
Alan Cox4da1a172008-07-22 11:16:21 +01001091 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001092
Alan Cox4da1a172008-07-22 11:16:21 +01001093 /* Disable access to divisor latch */
1094 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1095 mos7720_port->shadowLCR = data;
1096 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001097
1098 return status;
1099}
1100
1101/*
1102 * change_port_settings
1103 * This routine is called to set the UART on the device to match
1104 * the specified new settings.
1105 */
Alan Cox95da3102008-07-22 11:09:07 +01001106static void change_port_settings(struct tty_struct *tty,
1107 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001108 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109{
1110 struct usb_serial_port *port;
1111 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001112 int baud;
1113 unsigned cflag;
1114 unsigned iflag;
1115 __u8 mask = 0xff;
1116 __u8 lData;
1117 __u8 lParity;
1118 __u8 lStop;
1119 int status;
1120 int port_number;
1121 char data;
1122
1123 if (mos7720_port == NULL)
1124 return ;
1125
1126 port = mos7720_port->port;
1127 serial = port->serial;
1128 port_number = port->number - port->serial->minor;
1129
Harvey Harrison441b62c2008-03-03 16:08:34 -08001130 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001131
1132 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001133 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001134 return;
1135 }
1136
Harvey Harrison441b62c2008-03-03 16:08:34 -08001137 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001138
1139 lData = UART_LCR_WLEN8;
1140 lStop = 0x00; /* 1 stop bit */
1141 lParity = 0x00; /* No parity */
1142
1143 cflag = tty->termios->c_cflag;
1144 iflag = tty->termios->c_iflag;
1145
1146 /* Change the number of bits */
1147 switch (cflag & CSIZE) {
1148 case CS5:
1149 lData = UART_LCR_WLEN5;
1150 mask = 0x1f;
1151 break;
1152
1153 case CS6:
1154 lData = UART_LCR_WLEN6;
1155 mask = 0x3f;
1156 break;
1157
1158 case CS7:
1159 lData = UART_LCR_WLEN7;
1160 mask = 0x7f;
1161 break;
1162 default:
1163 case CS8:
1164 lData = UART_LCR_WLEN8;
1165 break;
1166 }
1167
1168 /* Change the Parity bit */
1169 if (cflag & PARENB) {
1170 if (cflag & PARODD) {
1171 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001173 } else {
1174 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001175 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001176 }
1177
1178 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001179 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001180 }
1181
1182 if (cflag & CMSPAR)
1183 lParity = lParity | 0x20;
1184
1185 /* Change the Stop bit */
1186 if (cflag & CSTOPB) {
1187 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001188 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001189 } else {
1190 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001191 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001192 }
1193
1194#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1195#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1196#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1197
1198 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001199 mos7720_port->shadowLCR &=
1200 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001201 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1202
1203
1204 /* Disable Interrupts */
1205 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001206 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1207 UART_IER, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208
1209 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001210 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001211
1212 data = 0xcf;
Alan Cox4da1a172008-07-22 11:16:21 +01001213 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214
1215 /* Send the updated LCR value to the mos7720 */
1216 data = mos7720_port->shadowLCR;
Alan Cox4da1a172008-07-22 11:16:21 +01001217 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001218
Alan Cox4da1a172008-07-22 11:16:21 +01001219 data = 0x00b;
1220 mos7720_port->shadowMCR = data;
1221 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1222 data = 0x00b;
1223 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001224
1225 /* set up the MCR register and send it to the mos7720 */
1226 mos7720_port->shadowMCR = UART_MCR_OUT2;
1227 if (cflag & CBAUD)
1228 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1229
1230 if (cflag & CRTSCTS) {
1231 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001232 /* To set hardware flow control to the specified *
1233 * serial port, in SP1/2_CONTROL_REG */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001234 if (port->number) {
1235 data = 0x001;
1236 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1237 0x08, &data);
1238 } else {
1239 data = 0x002;
1240 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1241 0x08, &data);
1242 }
1243 } else {
1244 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1245 }
1246
1247 data = mos7720_port->shadowMCR;
1248 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1249
1250 /* Determine divisor based on baud rate */
1251 baud = tty_get_baud_rate(tty);
1252 if (!baud) {
1253 /* pick a default, any default... */
1254 dbg("Picked default baud...");
1255 baud = 9600;
1256 }
1257
1258 if (baud >= 230400) {
1259 set_higher_rates(mos7720_port, baud);
1260 /* Enable Interrupts */
1261 data = 0x0c;
1262 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1263 return;
1264 }
1265
Harvey Harrison441b62c2008-03-03 16:08:34 -08001266 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001267 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001268 /* FIXME: needs to write actual resulting baud back not just
1269 blindly do so */
1270 if (cflag & CBAUD)
1271 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001272 /* Enable Interrupts */
1273 data = 0x0c;
1274 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1275
1276 if (port->read_urb->status != -EINPROGRESS) {
1277 port->read_urb->dev = serial->dev;
1278
1279 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1280 if (status)
1281 dbg("usb_submit_urb(read bulk) failed, status = %d",
1282 status);
1283 }
1284 return;
1285}
1286
1287/*
1288 * mos7720_set_termios
1289 * this function is called by the tty driver when it wants to change the
1290 * termios structure.
1291 */
Alan Cox95da3102008-07-22 11:09:07 +01001292static void mos7720_set_termios(struct tty_struct *tty,
1293 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001294{
1295 int status;
1296 unsigned int cflag;
1297 struct usb_serial *serial;
1298 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001299
1300 serial = port->serial;
1301
1302 mos7720_port = usb_get_serial_port_data(port);
1303
1304 if (mos7720_port == NULL)
1305 return;
1306
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001307 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001308 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001309 return;
1310 }
1311
Alan Cox4da1a172008-07-22 11:16:21 +01001312 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001313
1314 cflag = tty->termios->c_cflag;
1315
Harvey Harrison441b62c2008-03-03 16:08:34 -08001316 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001317 tty->termios->c_cflag,
1318 RELEVANT_IFLAG(tty->termios->c_iflag));
1319
Harvey Harrison441b62c2008-03-03 16:08:34 -08001320 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063a2008-01-03 17:01:18 +00001321 old_termios->c_cflag,
1322 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001323
Harvey Harrison441b62c2008-03-03 16:08:34 -08001324 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001325
1326 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001327 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001328
Alan Cox4da1a172008-07-22 11:16:21 +01001329 if (!port->read_urb) {
1330 dbg("%s", "URB KILLED !!!!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001331 return;
1332 }
1333
Alan Cox4da1a172008-07-22 11:16:21 +01001334 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001335 port->read_urb->dev = serial->dev;
1336 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1337 if (status)
1338 dbg("usb_submit_urb(read bulk) failed, status = %d",
1339 status);
1340 }
1341 return;
1342}
1343
1344/*
1345 * get_lsr_info - get line status register info
1346 *
1347 * Purpose: Let user call ioctl() to get info when the UART physically
1348 * is emptied. On bus types like RS485, the transmitter must
1349 * release the bus after transmitting. This must be done when
1350 * the transmit shift register is empty, not be done when the
1351 * transmit holding register is empty. This functionality
1352 * allows an RS485 driver to be written in user space.
1353 */
Alan Cox4da1a172008-07-22 11:16:21 +01001354static int get_lsr_info(struct tty_struct *tty,
1355 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001356{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001357 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001358 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001359 unsigned char data = 0;
1360 int port_number = port->number - port->serial->minor;
1361 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001362
Alan Cox95da3102008-07-22 11:09:07 +01001363 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001364 if (count == 0) {
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001365 send_mos_cmd(port->serial, MOS_READ, port_number,
1366 UART_LSR, &data);
1367 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1368 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1369 dbg("%s -- Empty", __func__);
1370 result = TIOCSER_TEMT;
1371 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001372 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001373 if (copy_to_user(value, &result, sizeof(int)))
1374 return -EFAULT;
1375 return 0;
1376}
1377
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001378static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1379{
1380 struct usb_serial_port *port = tty->driver_data;
1381 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1382 unsigned int result = 0;
1383 unsigned int mcr ;
1384 unsigned int msr ;
1385
1386 dbg("%s - port %d", __func__, port->number);
1387
1388 mcr = mos7720_port->shadowMCR;
1389 msr = mos7720_port->shadowMSR;
1390
1391 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1392 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1393 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1394 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1395 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1396 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1397
1398 dbg("%s -- %x", __func__, result);
1399
1400 return result;
1401}
1402
1403static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1404 unsigned int set, unsigned int clear)
1405{
1406 struct usb_serial_port *port = tty->driver_data;
1407 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1408 unsigned int mcr ;
1409 unsigned char lmcr;
1410
1411 dbg("%s - port %d", __func__, port->number);
1412 dbg("he was at tiocmget");
1413
1414 mcr = mos7720_port->shadowMCR;
1415
1416 if (set & TIOCM_RTS)
1417 mcr |= UART_MCR_RTS;
1418 if (set & TIOCM_DTR)
1419 mcr |= UART_MCR_DTR;
1420 if (set & TIOCM_LOOP)
1421 mcr |= UART_MCR_LOOP;
1422
1423 if (clear & TIOCM_RTS)
1424 mcr &= ~UART_MCR_RTS;
1425 if (clear & TIOCM_DTR)
1426 mcr &= ~UART_MCR_DTR;
1427 if (clear & TIOCM_LOOP)
1428 mcr &= ~UART_MCR_LOOP;
1429
1430 mos7720_port->shadowMCR = mcr;
1431 lmcr = mos7720_port->shadowMCR;
1432
1433 send_mos_cmd(port->serial, MOS_WRITE,
1434 port->number - port->serial->minor, UART_MCR, &lmcr);
1435
1436 return 0;
1437}
1438
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001439static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1440 unsigned int __user *value)
1441{
1442 unsigned int mcr ;
1443 unsigned int arg;
1444 unsigned char data;
1445
1446 struct usb_serial_port *port;
1447
1448 if (mos7720_port == NULL)
1449 return -1;
1450
Alan Cox4da1a172008-07-22 11:16:21 +01001451 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001452 mcr = mos7720_port->shadowMCR;
1453
1454 if (copy_from_user(&arg, value, sizeof(int)))
1455 return -EFAULT;
1456
1457 switch (cmd) {
1458 case TIOCMBIS:
1459 if (arg & TIOCM_RTS)
1460 mcr |= UART_MCR_RTS;
1461 if (arg & TIOCM_DTR)
1462 mcr |= UART_MCR_RTS;
1463 if (arg & TIOCM_LOOP)
1464 mcr |= UART_MCR_LOOP;
1465 break;
1466
1467 case TIOCMBIC:
1468 if (arg & TIOCM_RTS)
1469 mcr &= ~UART_MCR_RTS;
1470 if (arg & TIOCM_DTR)
1471 mcr &= ~UART_MCR_RTS;
1472 if (arg & TIOCM_LOOP)
1473 mcr &= ~UART_MCR_LOOP;
1474 break;
1475
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001476 }
1477
1478 mos7720_port->shadowMCR = mcr;
1479
1480 data = mos7720_port->shadowMCR;
1481 send_mos_cmd(port->serial, MOS_WRITE,
1482 port->number - port->serial->minor, UART_MCR, &data);
1483
1484 return 0;
1485}
1486
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001487static int get_serial_info(struct moschip_port *mos7720_port,
1488 struct serial_struct __user *retinfo)
1489{
1490 struct serial_struct tmp;
1491
1492 if (!retinfo)
1493 return -EFAULT;
1494
1495 memset(&tmp, 0, sizeof(tmp));
1496
1497 tmp.type = PORT_16550A;
1498 tmp.line = mos7720_port->port->serial->minor;
1499 tmp.port = mos7720_port->port->number;
1500 tmp.irq = 0;
1501 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001502 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001503 tmp.baud_base = 9600;
1504 tmp.close_delay = 5*HZ;
1505 tmp.closing_wait = 30*HZ;
1506
1507 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1508 return -EFAULT;
1509 return 0;
1510}
1511
Alan Cox95da3102008-07-22 11:09:07 +01001512static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001513 unsigned int cmd, unsigned long arg)
1514{
Alan Cox95da3102008-07-22 11:09:07 +01001515 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001516 struct moschip_port *mos7720_port;
1517 struct async_icount cnow;
1518 struct async_icount cprev;
1519 struct serial_icounter_struct icount;
1520
1521 mos7720_port = usb_get_serial_port_data(port);
1522 if (mos7720_port == NULL)
1523 return -ENODEV;
1524
Harvey Harrison441b62c2008-03-03 16:08:34 -08001525 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001526
1527 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001528 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001529 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01001530 return get_lsr_info(tty, mos7720_port,
1531 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001532 return 0;
1533
Alan Cox95da3102008-07-22 11:09:07 +01001534 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001535 case TIOCMBIS:
1536 case TIOCMBIC:
Alan Cox4da1a172008-07-22 11:16:21 +01001537 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1538 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001539 return set_modem_info(mos7720_port, cmd,
1540 (unsigned int __user *)arg);
1541
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001542 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001543 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001544 return get_serial_info(mos7720_port,
1545 (struct serial_struct __user *)arg);
1546
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001547 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001548 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001549 cprev = mos7720_port->icount;
1550 while (1) {
1551 if (signal_pending(current))
1552 return -ERESTARTSYS;
1553 cnow = mos7720_port->icount;
1554 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1555 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1556 return -EIO; /* no change => error */
1557 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1558 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1559 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001560 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001561 return 0;
1562 }
1563 cprev = cnow;
1564 }
1565 /* NOTREACHED */
1566 break;
1567
1568 case TIOCGICOUNT:
1569 cnow = mos7720_port->icount;
1570 icount.cts = cnow.cts;
1571 icount.dsr = cnow.dsr;
1572 icount.rng = cnow.rng;
1573 icount.dcd = cnow.dcd;
1574 icount.rx = cnow.rx;
1575 icount.tx = cnow.tx;
1576 icount.frame = cnow.frame;
1577 icount.overrun = cnow.overrun;
1578 icount.parity = cnow.parity;
1579 icount.brk = cnow.brk;
1580 icount.buf_overrun = cnow.buf_overrun;
1581
Harvey Harrison441b62c2008-03-03 16:08:34 -08001582 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Alan Cox4da1a172008-07-22 11:16:21 +01001583 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001584 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1585 return -EFAULT;
1586 return 0;
1587 }
1588
1589 return -ENOIOCTLCMD;
1590}
1591
1592static int mos7720_startup(struct usb_serial *serial)
1593{
1594 struct moschip_serial *mos7720_serial;
1595 struct moschip_port *mos7720_port;
1596 struct usb_device *dev;
1597 int i;
1598 char data;
Mike Dunnfb088e32010-01-26 12:12:12 -05001599 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001600
Harvey Harrison441b62c2008-03-03 16:08:34 -08001601 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001602
1603 if (!serial) {
1604 dbg("Invalid Handler");
1605 return -ENODEV;
1606 }
1607
1608 dev = serial->dev;
1609
1610 /* create our private serial structure */
1611 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1612 if (mos7720_serial == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001613 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001614 return -ENOMEM;
1615 }
1616
1617 usb_set_serial_data(serial, mos7720_serial);
1618
Mike Dunnfb088e32010-01-26 12:12:12 -05001619 /*
1620 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1621 * port, and the second for the serial port. Because the usbserial core
1622 * assumes both pairs are serial ports, we must engage in a bit of
1623 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1624 * port 0 point to the serial port. However, both moschip devices use a
1625 * single interrupt-in endpoint for both ports (as mentioned a little
1626 * further down), and this endpoint was assigned to port 0. So after
1627 * the swap, we must copy the interrupt endpoint elements from port 1
1628 * (as newly assigned) to port 0, and null out port 1 pointers.
1629 */
1630 if (product == MOSCHIP_DEVICE_ID_7715) {
1631 struct usb_serial_port *tmp = serial->port[0];
1632 serial->port[0] = serial->port[1];
1633 serial->port[1] = tmp;
1634 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1635 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1636 serial->port[0]->interrupt_in_endpointAddress =
1637 tmp->interrupt_in_endpointAddress;
1638 serial->port[1]->interrupt_in_urb = NULL;
1639 serial->port[1]->interrupt_in_buffer = NULL;
1640 }
1641
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001642 /* we set up the pointers to the endpoints in the mos7720_open *
1643 * function, as the structures aren't created yet. */
1644
1645 /* set up port private structures */
1646 for (i = 0; i < serial->num_ports; ++i) {
1647 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1648 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001649 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001650 usb_set_serial_data(serial, NULL);
1651 kfree(mos7720_serial);
1652 return -ENOMEM;
1653 }
1654
1655 /* Initialize all port interrupt end point to port 0 int
1656 * endpoint. Our device has only one interrupt endpoint
Mike Dunnfb088e32010-01-26 12:12:12 -05001657 * common to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01001658 serial->port[i]->interrupt_in_endpointAddress =
1659 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001660
1661 mos7720_port->port = serial->port[i];
1662 usb_set_serial_port_data(serial->port[i], mos7720_port);
1663
1664 dbg("port number is %d", serial->port[i]->number);
1665 dbg("serial number is %d", serial->minor);
1666 }
1667
1668
1669 /* setting configuration feature to one */
1670 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001671 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001672
Alan Cox4da1a172008-07-22 11:16:21 +01001673 /* LSR For Port 1 */
1674 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1675 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001676
Alan Cox4da1a172008-07-22 11:16:21 +01001677 /* LSR For Port 2 */
1678 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1679 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001680
1681 return 0;
1682}
1683
Alan Sternf9c99bb2009-06-02 11:53:55 -04001684static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001685{
1686 int i;
1687
1688 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04001689 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001690 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001691
1692 /* free private structure allocated for serial device */
1693 kfree(usb_get_serial_data(serial));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001694}
1695
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001696static struct usb_driver usb_driver = {
1697 .name = "moschip7720",
1698 .probe = usb_serial_probe,
1699 .disconnect = usb_serial_disconnect,
1700 .id_table = moschip_port_id_table,
1701 .no_dynamic_id = 1,
1702};
1703
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001704static struct usb_serial_driver moschip7720_2port_driver = {
1705 .driver = {
1706 .owner = THIS_MODULE,
1707 .name = "moschip7720",
1708 },
1709 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001710 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001711 .id_table = moschip_port_id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05001712 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001713 .open = mos7720_open,
1714 .close = mos7720_close,
1715 .throttle = mos7720_throttle,
1716 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05001717 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001718 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001719 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001720 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001721 .tiocmget = mos7720_tiocmget,
1722 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723 .set_termios = mos7720_set_termios,
1724 .write = mos7720_write,
1725 .write_room = mos7720_write_room,
1726 .chars_in_buffer = mos7720_chars_in_buffer,
1727 .break_ctl = mos7720_break,
1728 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05001729 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001730};
1731
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001732static int __init moschip7720_init(void)
1733{
1734 int retval;
1735
Harvey Harrison441b62c2008-03-03 16:08:34 -08001736 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001737
1738 /* Register with the usb serial */
1739 retval = usb_serial_register(&moschip7720_2port_driver);
1740 if (retval)
1741 goto failed_port_device_register;
1742
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001743 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1744 DRIVER_DESC "\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001745
1746 /* Register with the usb */
1747 retval = usb_register(&usb_driver);
1748 if (retval)
1749 goto failed_usb_register;
1750
1751 return 0;
1752
1753failed_usb_register:
1754 usb_serial_deregister(&moschip7720_2port_driver);
1755
1756failed_port_device_register:
1757 return retval;
1758}
1759
1760static void __exit moschip7720_exit(void)
1761{
1762 usb_deregister(&usb_driver);
1763 usb_serial_deregister(&moschip7720_2port_driver);
1764}
1765
1766module_init(moschip7720_init);
1767module_exit(moschip7720_exit);
1768
1769/* Module information */
Alan Cox4da1a172008-07-22 11:16:21 +01001770MODULE_AUTHOR(DRIVER_AUTHOR);
1771MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001772MODULE_LICENSE("GPL");
1773
1774module_param(debug, bool, S_IRUGO | S_IWUSR);
1775MODULE_PARM_DESC(debug, "Debug enabled or not");