blob: 24e3b5d4b4d49f088a9891718bc36c815c4a409c [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
84#define USB_VENDOR_ID_MOSCHIP 0x9710
85#define MOSCHIP_DEVICE_ID_7720 0x7720
86#define MOSCHIP_DEVICE_ID_7715 0x7715
87
88static struct usb_device_id moschip_port_id_table [] = {
Alan Cox4da1a172008-07-22 11:16:21 +010089 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070090 { } /* terminating entry */
91};
92MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
93
94
95/*
96 * mos7720_interrupt_callback
97 * this is the callback function for when we have received data on the
98 * interrupt endpoint.
99 */
100static void mos7720_interrupt_callback(struct urb *urb)
101{
102 int result;
103 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700104 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100105 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700106 __u8 sp1;
107 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700108
Alan Cox4da1a172008-07-22 11:16:21 +0100109 dbg("%s", " : Entering\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700110
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700111 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700112 case 0:
113 /* success */
114 break;
115 case -ECONNRESET:
116 case -ENOENT:
117 case -ESHUTDOWN:
118 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800119 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700120 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700121 return;
122 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800123 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700124 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700125 goto exit;
126 }
127
128 length = urb->actual_length;
129 data = urb->transfer_buffer;
130
131 /* Moschip get 4 bytes
132 * Byte 1 IIR Port 1 (port.number is 0)
133 * Byte 2 IIR Port 2 (port.number is 1)
134 * Byte 3 --------------
135 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100136
137 /* the above description is inverted
138 * oneukum 2007-03-14 */
139
140 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700141 dbg("Wrong data !!!");
142 return;
143 }
144
Oliver Neukum325b70c2007-03-19 13:58:29 +0100145 sp1 = data[3];
146 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700147
Oliver Neukum325b70c2007-03-19 13:58:29 +0100148 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700149 /* No Interrupt Pending in both the ports */
150 dbg("No Interrupt !!!");
151 } else {
152 switch (sp1 & 0x0f) {
153 case SERIAL_IIR_RLS:
154 dbg("Serial Port 1: Receiver status error or address "
155 "bit detected in 9-bit mode\n");
156 break;
157 case SERIAL_IIR_CTI:
158 dbg("Serial Port 1: Receiver time out");
159 break;
160 case SERIAL_IIR_MS:
161 dbg("Serial Port 1: Modem status change");
162 break;
163 }
164
165 switch (sp2 & 0x0f) {
166 case SERIAL_IIR_RLS:
167 dbg("Serial Port 2: Receiver status error or address "
168 "bit detected in 9-bit mode");
169 break;
170 case SERIAL_IIR_CTI:
171 dbg("Serial Port 2: Receiver time out");
172 break;
173 case SERIAL_IIR_MS:
174 dbg("Serial Port 2: Modem status change");
175 break;
176 }
177 }
178
179exit:
180 result = usb_submit_urb(urb, GFP_ATOMIC);
181 if (result)
182 dev_err(&urb->dev->dev,
183 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800184 __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700185 return;
186}
187
188/*
189 * mos7720_bulk_in_callback
190 * this is the callback function for when we have received data on the
191 * bulk in endpoint.
192 */
193static void mos7720_bulk_in_callback(struct urb *urb)
194{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700195 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700196 unsigned char *data ;
197 struct usb_serial_port *port;
198 struct moschip_port *mos7720_port;
199 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700200 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700201
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700202 if (status) {
203 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700204 return;
205 }
206
207 mos7720_port = urb->context;
208 if (!mos7720_port) {
Alan Cox4da1a172008-07-22 11:16:21 +0100209 dbg("%s", "NULL mos7720_port pointer \n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700210 return ;
211 }
212
213 port = mos7720_port->port;
214
Harvey Harrison441b62c2008-03-03 16:08:34 -0800215 dbg("Entering...%s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700216
217 data = urb->transfer_buffer;
218
Alan Cox4a90f092008-10-13 10:39:46 +0100219 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700220 if (tty && urb->actual_length) {
221 tty_buffer_request_room(tty, urb->actual_length);
222 tty_insert_flip_string(tty, data, urb->actual_length);
223 tty_flip_buffer_push(tty);
224 }
Alan Cox4a90f092008-10-13 10:39:46 +0100225 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700226
227 if (!port->read_urb) {
228 dbg("URB KILLED !!!");
229 return;
230 }
231
232 if (port->read_urb->status != -EINPROGRESS) {
233 port->read_urb->dev = port->serial->dev;
234
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700235 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
236 if (retval)
237 dbg("usb_submit_urb(read bulk) failed, retval = %d",
238 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700239 }
240}
241
242/*
243 * mos7720_bulk_out_data_callback
244 * this is the callback function for when we have finished sending serial
245 * data on the bulk out endpoint.
246 */
247static void mos7720_bulk_out_data_callback(struct urb *urb)
248{
249 struct moschip_port *mos7720_port;
250 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700251 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700252
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700253 if (status) {
254 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700255 return;
256 }
257
258 mos7720_port = urb->context;
259 if (!mos7720_port) {
260 dbg("NULL mos7720_port pointer");
261 return ;
262 }
263
264 dbg("Entering .........");
265
Alan Cox4a90f092008-10-13 10:39:46 +0100266 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700267
Jiri Slabyb963a842007-02-10 01:44:55 -0800268 if (tty && mos7720_port->open)
269 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100270 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700271}
272
273/*
274 * send_mos_cmd
275 * this function will be used for sending command to device
276 */
277static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
278 __u16 index, void *data)
279{
280 int status;
281 unsigned int pipe;
282 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
283 __u8 requesttype;
284 __u16 size = 0x0000;
285
286 if (value < MOS_MAX_PORT) {
Alan Cox4da1a172008-07-22 11:16:21 +0100287 if (product == MOSCHIP_DEVICE_ID_7715)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700288 value = value*0x100+0x100;
Alan Cox4da1a172008-07-22 11:16:21 +0100289 else
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700290 value = value*0x100+0x200;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700291 } else {
292 value = 0x0000;
293 if ((product == MOSCHIP_DEVICE_ID_7715) &&
294 (index != 0x08)) {
295 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
Alan Cox4da1a172008-07-22 11:16:21 +0100296 /* index = 0x01 ; */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700297 }
298 }
299
300 if (request == MOS_WRITE) {
301 request = (__u8)MOS_WRITE;
302 requesttype = (__u8)0x40;
303 value = value + (__u16)*((unsigned char *)data);
304 data = NULL;
305 pipe = usb_sndctrlpipe(serial->dev, 0);
306 } else {
307 request = (__u8)MOS_READ;
308 requesttype = (__u8)0xC0;
309 size = 0x01;
Alan Cox4da1a172008-07-22 11:16:21 +0100310 pipe = usb_rcvctrlpipe(serial->dev, 0);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700311 }
312
313 status = usb_control_msg(serial->dev, pipe, request, requesttype,
314 value, index, data, size, MOS_WDR_TIMEOUT);
315
316 if (status < 0)
Alan Cox4da1a172008-07-22 11:16:21 +0100317 dbg("Command Write failed Value %x index %x\n", value, index);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700318
319 return status;
320}
321
Alan Cox95da3102008-07-22 11:09:07 +0100322static int mos7720_open(struct tty_struct *tty,
Alan Cox4da1a172008-07-22 11:16:21 +0100323 struct usb_serial_port *port, struct file *filp)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700324{
325 struct usb_serial *serial;
326 struct usb_serial_port *port0;
327 struct urb *urb;
328 struct moschip_serial *mos7720_serial;
329 struct moschip_port *mos7720_port;
330 int response;
331 int port_number;
332 char data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100333 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700334 int j;
335
336 serial = port->serial;
337
338 mos7720_port = usb_get_serial_port_data(port);
339 if (mos7720_port == NULL)
340 return -ENODEV;
341
342 port0 = serial->port[0];
343
344 mos7720_serial = usb_get_serial_data(serial);
345
346 if (mos7720_serial == NULL || port0 == NULL)
347 return -ENODEV;
348
349 usb_clear_halt(serial->dev, port->write_urb->pipe);
350 usb_clear_halt(serial->dev, port->read_urb->pipe);
351
352 /* Initialising the write urb pool */
353 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +0100354 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700355 mos7720_port->write_urb_pool[j] = urb;
356
357 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700358 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700359 continue;
360 }
361
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 GFP_KERNEL);
364 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700365 dev_err(&port->dev,
366 "%s-out of memory for urb buffers.\n",
367 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100368 usb_free_urb(mos7720_port->write_urb_pool[j]);
369 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700370 continue;
371 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100372 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700373 }
374
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100375 if (!allocated_urbs)
376 return -ENOMEM;
377
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700378 /* Initialize MCS7720 -- Write Init values to corresponding Registers
379 *
380 * Register Index
381 * 1 : IER
382 * 2 : FCR
383 * 3 : LCR
384 * 4 : MCR
385 *
386 * 0x08 : SP1/2 Control Reg
387 */
388 port_number = port->number - port->serial->minor;
389 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +0100390 dbg("SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700391
392 dbg("Check:Sending Command ..........");
393
394 data = 0x02;
395 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
396 data = 0x02;
397 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
398
399 data = 0x00;
400 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
401 data = 0x00;
402 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
403
404 data = 0xCF;
405 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
406 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100407 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700408 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
409 data = 0x0b;
Alan Cox4da1a172008-07-22 11:16:21 +0100410 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
412 data = 0x0b;
413 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
414
415 data = 0x00;
416 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
417 data = 0x00;
418 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
419
420/* data = 0x00;
421 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
422 data = 0x03;
423 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
424 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100425 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
426 port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700427*/
428 data = 0x00;
429 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
430
431 data = data | (port->number - port->serial->minor + 1);
432 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
433
434 data = 0x83;
Alan Cox4da1a172008-07-22 11:16:21 +0100435 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700436 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
437 data = 0x0c;
438 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
439 data = 0x00;
440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
441 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100442 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700443 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
444 data = 0x0c;
445 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
446 data = 0x0c;
447 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
448
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700449 /* see if we've set up our endpoint info yet *
450 * (can't set it up in mos7720_startup as the *
451 * structures were not set up at that time.) */
452 if (!mos7720_serial->interrupt_started) {
453 dbg("Interrupt buffer NULL !!!");
454
455 /* not set up yet, so do it now */
456 mos7720_serial->interrupt_started = 1;
457
458 dbg("To Submit URB !!!");
459
460 /* set up our interrupt urb */
461 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100462 usb_rcvintpipe(serial->dev,
463 port->interrupt_in_endpointAddress),
464 port0->interrupt_in_buffer,
465 port0->interrupt_in_urb->transfer_buffer_length,
466 mos7720_interrupt_callback, mos7720_port,
467 port0->interrupt_in_urb->interval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700468
469 /* start interrupt read for this mos7720 this interrupt *
Alan Cox4da1a172008-07-22 11:16:21 +0100470 * will continue as long as the mos7720 is connected */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700471 dbg("Submit URB over !!!");
472 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
473 if (response)
474 dev_err(&port->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700475 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800476 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700477 }
478
479 /* set up our bulk in urb */
480 usb_fill_bulk_urb(port->read_urb, serial->dev,
481 usb_rcvbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100482 port->bulk_in_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700483 port->bulk_in_buffer,
484 port->read_urb->transfer_buffer_length,
485 mos7720_bulk_in_callback, mos7720_port);
486 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
487 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100488 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
489 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700490
491 /* initialize our icount structure */
492 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
493
494 /* initialize our port settings */
495 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
496
497 /* send a open port command */
498 mos7720_port->open = 1;
499
500 return 0;
501}
502
503/*
504 * mos7720_chars_in_buffer
505 * this function is called by the tty driver when it wants to know how many
506 * bytes of data we currently have outstanding in the port (data that has
507 * been written, but hasn't made it out the port yet)
508 * If successful, we return the number of bytes left to be written in the
509 * system,
510 * Otherwise we return a negative error number.
511 */
Alan Cox95da3102008-07-22 11:09:07 +0100512static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700513{
Alan Cox95da3102008-07-22 11:09:07 +0100514 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700515 int i;
516 int chars = 0;
517 struct moschip_port *mos7720_port;
518
Harvey Harrison441b62c2008-03-03 16:08:34 -0800519 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700520
521 mos7720_port = usb_get_serial_port_data(port);
522 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800523 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700524 return -ENODEV;
525 }
526
527 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100528 if (mos7720_port->write_urb_pool[i] &&
529 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700530 chars += URB_TRANSFER_BUFFER_SIZE;
531 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800532 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700533 return chars;
534}
535
Alan Cox95da3102008-07-22 11:09:07 +0100536static void mos7720_close(struct tty_struct *tty,
537 struct usb_serial_port *port, struct file *filp)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700538{
539 struct usb_serial *serial;
540 struct moschip_port *mos7720_port;
541 char data;
542 int j;
543
544 dbg("mos7720_close:entering...");
545
546 serial = port->serial;
547
548 mos7720_port = usb_get_serial_port_data(port);
549 if (mos7720_port == NULL)
550 return;
551
552 for (j = 0; j < NUM_URBS; ++j)
553 usb_kill_urb(mos7720_port->write_urb_pool[j]);
554
555 /* Freeing Write URBs */
556 for (j = 0; j < NUM_URBS; ++j) {
557 if (mos7720_port->write_urb_pool[j]) {
558 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
559 usb_free_urb(mos7720_port->write_urb_pool[j]);
560 }
561 }
562
563 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100564 * and interrupt read if they exists, otherwise nop */
565 dbg("Shutdown bulk write");
566 usb_kill_urb(port->write_urb);
567 dbg("Shutdown bulk read");
568 usb_kill_urb(port->read_urb);
569
570 mutex_lock(&serial->disc_mutex);
571 /* these commands must not be issued if the device has
572 * been disconnected */
573 if (!serial->disconnected) {
574 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100575 send_mos_cmd(serial, MOS_WRITE,
576 port->number - port->serial->minor, 0x04, &data);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100577
578 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100579 send_mos_cmd(serial, MOS_WRITE,
580 port->number - port->serial->minor, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700581 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100582 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700583 mos7720_port->open = 0;
584
Harvey Harrison441b62c2008-03-03 16:08:34 -0800585 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700586}
587
Alan Cox95da3102008-07-22 11:09:07 +0100588static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700589{
Alan Cox95da3102008-07-22 11:09:07 +0100590 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +0100591 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700592 struct usb_serial *serial;
593 struct moschip_port *mos7720_port;
594
Harvey Harrison441b62c2008-03-03 16:08:34 -0800595 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700596
597 serial = port->serial;
598
599 mos7720_port = usb_get_serial_port_data(port);
600 if (mos7720_port == NULL)
601 return;
602
603 if (break_state == -1)
604 data = mos7720_port->shadowLCR | UART_LCR_SBC;
605 else
606 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
607
608 mos7720_port->shadowLCR = data;
609 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
610 0x03, &data);
611
612 return;
613}
614
615/*
616 * mos7720_write_room
617 * this function is called by the tty driver when it wants to know how many
618 * bytes of data we can accept for a specific port.
619 * If successful, we return the amount of room that we have for this port
620 * Otherwise we return a negative error number.
621 */
Alan Cox95da3102008-07-22 11:09:07 +0100622static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700623{
Alan Cox95da3102008-07-22 11:09:07 +0100624 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700625 struct moschip_port *mos7720_port;
626 int room = 0;
627 int i;
628
Harvey Harrison441b62c2008-03-03 16:08:34 -0800629 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700630
631 mos7720_port = usb_get_serial_port_data(port);
632 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800633 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700634 return -ENODEV;
635 }
636
Alan Coxa5b6f602008-04-08 17:16:06 +0100637 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700638 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100639 if (mos7720_port->write_urb_pool[i] &&
640 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700641 room += URB_TRANSFER_BUFFER_SIZE;
642 }
643
Harvey Harrison441b62c2008-03-03 16:08:34 -0800644 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700645 return room;
646}
647
Alan Cox95da3102008-07-22 11:09:07 +0100648static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
649 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700650{
651 int status;
652 int i;
653 int bytes_sent = 0;
654 int transfer_size;
655
656 struct moschip_port *mos7720_port;
657 struct usb_serial *serial;
658 struct urb *urb;
659 const unsigned char *current_position = data;
660
Harvey Harrison441b62c2008-03-03 16:08:34 -0800661 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700662
663 serial = port->serial;
664
665 mos7720_port = usb_get_serial_port_data(port);
666 if (mos7720_port == NULL) {
667 dbg("mos7720_port is NULL");
668 return -ENODEV;
669 }
670
671 /* try to find a free urb in the list */
672 urb = NULL;
673
674 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100675 if (mos7720_port->write_urb_pool[i] &&
676 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700677 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +0100678 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700679 break;
680 }
681 }
682
683 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800684 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700685 goto exit;
686 }
687
688 if (urb->transfer_buffer == NULL) {
689 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
690 GFP_KERNEL);
691 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700692 dev_err(&port->dev, "%s no more kernel memory...\n",
693 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700694 goto exit;
695 }
696 }
Alan Cox4da1a172008-07-22 11:16:21 +0100697 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700698
699 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800700 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700701 urb->transfer_buffer);
702
703 /* fill urb with data and submit */
704 usb_fill_bulk_urb(urb, serial->dev,
705 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100706 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700707 urb->transfer_buffer, transfer_size,
708 mos7720_bulk_out_data_callback, mos7720_port);
709
710 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +0100711 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700712 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700713 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
714 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700715 bytes_sent = status;
716 goto exit;
717 }
718 bytes_sent = transfer_size;
719
720exit:
721 return bytes_sent;
722}
723
Alan Cox95da3102008-07-22 11:09:07 +0100724static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700725{
Alan Cox95da3102008-07-22 11:09:07 +0100726 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700727 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700728 int status;
729
Harvey Harrison441b62c2008-03-03 16:08:34 -0800730 dbg("%s- port %d\n", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700731
732 mos7720_port = usb_get_serial_port_data(port);
733
734 if (mos7720_port == NULL)
735 return;
736
737 if (!mos7720_port->open) {
738 dbg("port not opened");
739 return;
740 }
741
Harvey Harrison441b62c2008-03-03 16:08:34 -0800742 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700743
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700744 /* if we are implementing XON/XOFF, send the stop character */
745 if (I_IXOFF(tty)) {
746 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100747 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748 if (status <= 0)
749 return;
750 }
751
752 /* if we are implementing RTS/CTS, toggle that line */
753 if (tty->termios->c_cflag & CRTSCTS) {
754 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
755 status = send_mos_cmd(port->serial, MOS_WRITE,
756 port->number - port->serial->minor,
757 UART_MCR, &mos7720_port->shadowMCR);
758 if (status != 0)
759 return;
760 }
761}
762
Alan Cox95da3102008-07-22 11:09:07 +0100763static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700764{
Alan Cox95da3102008-07-22 11:09:07 +0100765 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700766 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100767 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700768
769 if (mos7720_port == NULL)
770 return;
771
772 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800773 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700774 return;
775 }
776
Harvey Harrison441b62c2008-03-03 16:08:34 -0800777 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700778
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700779 /* if we are implementing XON/XOFF, send the start character */
780 if (I_IXOFF(tty)) {
781 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100782 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700783 if (status <= 0)
784 return;
785 }
786
787 /* if we are implementing RTS/CTS, toggle that line */
788 if (tty->termios->c_cflag & CRTSCTS) {
789 mos7720_port->shadowMCR |= UART_MCR_RTS;
790 status = send_mos_cmd(port->serial, MOS_WRITE,
791 port->number - port->serial->minor,
792 UART_MCR, &mos7720_port->shadowMCR);
793 if (status != 0)
794 return;
795 }
796}
797
798static int set_higher_rates(struct moschip_port *mos7720_port,
799 unsigned int baud)
800{
801 unsigned char data;
802 struct usb_serial_port *port;
803 struct usb_serial *serial;
804 int port_number;
805
806 if (mos7720_port == NULL)
807 return -EINVAL;
808
809 port = mos7720_port->port;
810 serial = port->serial;
811
Alan Cox4da1a172008-07-22 11:16:21 +0100812 /***********************************************
813 * Init Sequence for higher rates
814 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700815 dbg("Sending Setting Commands ..........");
816 port_number = port->number - port->serial->minor;
817
818 data = 0x000;
819 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
820 data = 0x000;
821 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
822 data = 0x0CF;
823 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
824 data = 0x00b;
Alan Cox4da1a172008-07-22 11:16:21 +0100825 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700826 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
827 data = 0x00b;
828 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
829
830 data = 0x000;
831 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
832 data = 0x000;
833 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
834
835
Alan Cox4da1a172008-07-22 11:16:21 +0100836 /***********************************************
837 * Set for higher rates *
838 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700839
840 data = baud * 0x10;
Alan Cox4da1a172008-07-22 11:16:21 +0100841 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700842
843 data = 0x003;
844 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
845 data = 0x003;
846 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
847
848 data = 0x02b;
Alan Cox4da1a172008-07-22 11:16:21 +0100849 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700850 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
851 data = 0x02b;
852 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
853
Alan Cox4da1a172008-07-22 11:16:21 +0100854 /***********************************************
855 * Set DLL/DLM
856 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700857
858 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100859 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700860 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
861
862 data = 0x001; /* DLL */
Alan Cox4da1a172008-07-22 11:16:21 +0100863 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700864 data = 0x000; /* DLM */
Alan Cox4da1a172008-07-22 11:16:21 +0100865 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700866
867 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100868 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700869 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
870
871 return 0;
872}
873
874/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +0100875struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700876 __u32 baudrate;
877 __u16 divisor;
878};
879
880/* Define table of divisors for moschip 7720 hardware *
881 * These assume a 3.6864MHz crystal, the standard /16, and *
882 * MCR.7 = 0. */
883static struct divisor_table_entry divisor_table[] = {
884 { 50, 2304},
885 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
886 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
887 { 150, 768},
888 { 300, 384},
889 { 600, 192},
890 { 1200, 96},
891 { 1800, 64},
892 { 2400, 48},
893 { 4800, 24},
894 { 7200, 16},
895 { 9600, 12},
896 { 19200, 6},
897 { 38400, 3},
898 { 57600, 2},
899 { 115200, 1},
900};
901
902/*****************************************************************************
903 * calc_baud_rate_divisor
904 * this function calculates the proper baud rate divisor for the specified
905 * baud rate.
906 *****************************************************************************/
907static int calc_baud_rate_divisor(int baudrate, int *divisor)
908{
909 int i;
910 __u16 custom;
911 __u16 round1;
912 __u16 round;
913
914
Harvey Harrison441b62c2008-03-03 16:08:34 -0800915 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700916
917 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
918 if (divisor_table[i].baudrate == baudrate) {
919 *divisor = divisor_table[i].divisor;
920 return 0;
921 }
922 }
923
Alan Cox4da1a172008-07-22 11:16:21 +0100924 /* After trying for all the standard baud rates *
925 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700926 if (baudrate > 75 && baudrate < 230400) {
927 /* get the divisor */
928 custom = (__u16)(230400L / baudrate);
929
930 /* Check for round off */
931 round1 = (__u16)(2304000L / baudrate);
932 round = (__u16)(round1 - (custom * 10));
933 if (round > 4)
934 custom++;
935 *divisor = custom;
936
Alan Cox4da1a172008-07-22 11:16:21 +0100937 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700938 return 0;
939 }
940
941 dbg("Baud calculation Failed...");
942 return -EINVAL;
943}
944
945/*
946 * send_cmd_write_baud_rate
947 * this function sends the proper command to change the baud rate of the
948 * specified port.
949 */
950static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
951 int baudrate)
952{
953 struct usb_serial_port *port;
954 struct usb_serial *serial;
955 int divisor;
956 int status;
957 unsigned char data;
958 unsigned char number;
959
960 if (mos7720_port == NULL)
961 return -1;
962
963 port = mos7720_port->port;
964 serial = port->serial;
965
Harvey Harrison441b62c2008-03-03 16:08:34 -0800966 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700967
968 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700970
Alan Cox4da1a172008-07-22 11:16:21 +0100971 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700972 status = calc_baud_rate_divisor(baudrate, &divisor);
973 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700974 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700975 return status;
976 }
977
Alan Cox4da1a172008-07-22 11:16:21 +0100978 /* Enable access to divisor latch */
979 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
980 mos7720_port->shadowLCR = data;
981 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700982
983 /* Write the divisor */
984 data = ((unsigned char)(divisor & 0xff));
Alan Cox4da1a172008-07-22 11:16:21 +0100985 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700986
987 data = ((unsigned char)((divisor & 0xff00) >> 8));
Alan Cox4da1a172008-07-22 11:16:21 +0100988 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700989
Alan Cox4da1a172008-07-22 11:16:21 +0100990 /* Disable access to divisor latch */
991 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
992 mos7720_port->shadowLCR = data;
993 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700994
995 return status;
996}
997
998/*
999 * change_port_settings
1000 * This routine is called to set the UART on the device to match
1001 * the specified new settings.
1002 */
Alan Cox95da3102008-07-22 11:09:07 +01001003static void change_port_settings(struct tty_struct *tty,
1004 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001005 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001006{
1007 struct usb_serial_port *port;
1008 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001009 int baud;
1010 unsigned cflag;
1011 unsigned iflag;
1012 __u8 mask = 0xff;
1013 __u8 lData;
1014 __u8 lParity;
1015 __u8 lStop;
1016 int status;
1017 int port_number;
1018 char data;
1019
1020 if (mos7720_port == NULL)
1021 return ;
1022
1023 port = mos7720_port->port;
1024 serial = port->serial;
1025 port_number = port->number - port->serial->minor;
1026
Harvey Harrison441b62c2008-03-03 16:08:34 -08001027 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001028
1029 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001031 return;
1032 }
1033
Harvey Harrison441b62c2008-03-03 16:08:34 -08001034 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001035
1036 lData = UART_LCR_WLEN8;
1037 lStop = 0x00; /* 1 stop bit */
1038 lParity = 0x00; /* No parity */
1039
1040 cflag = tty->termios->c_cflag;
1041 iflag = tty->termios->c_iflag;
1042
1043 /* Change the number of bits */
1044 switch (cflag & CSIZE) {
1045 case CS5:
1046 lData = UART_LCR_WLEN5;
1047 mask = 0x1f;
1048 break;
1049
1050 case CS6:
1051 lData = UART_LCR_WLEN6;
1052 mask = 0x3f;
1053 break;
1054
1055 case CS7:
1056 lData = UART_LCR_WLEN7;
1057 mask = 0x7f;
1058 break;
1059 default:
1060 case CS8:
1061 lData = UART_LCR_WLEN8;
1062 break;
1063 }
1064
1065 /* Change the Parity bit */
1066 if (cflag & PARENB) {
1067 if (cflag & PARODD) {
1068 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001069 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001070 } else {
1071 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073 }
1074
1075 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001076 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001077 }
1078
1079 if (cflag & CMSPAR)
1080 lParity = lParity | 0x20;
1081
1082 /* Change the Stop bit */
1083 if (cflag & CSTOPB) {
1084 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001085 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001086 } else {
1087 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001089 }
1090
1091#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1092#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1093#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1094
1095 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001096 mos7720_port->shadowLCR &=
1097 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001098 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1099
1100
1101 /* Disable Interrupts */
1102 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001103 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1104 UART_IER, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001105
1106 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001107 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001108
1109 data = 0xcf;
Alan Cox4da1a172008-07-22 11:16:21 +01001110 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001111
1112 /* Send the updated LCR value to the mos7720 */
1113 data = mos7720_port->shadowLCR;
Alan Cox4da1a172008-07-22 11:16:21 +01001114 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001115
Alan Cox4da1a172008-07-22 11:16:21 +01001116 data = 0x00b;
1117 mos7720_port->shadowMCR = data;
1118 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1119 data = 0x00b;
1120 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001121
1122 /* set up the MCR register and send it to the mos7720 */
1123 mos7720_port->shadowMCR = UART_MCR_OUT2;
1124 if (cflag & CBAUD)
1125 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1126
1127 if (cflag & CRTSCTS) {
1128 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001129 /* To set hardware flow control to the specified *
1130 * serial port, in SP1/2_CONTROL_REG */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001131 if (port->number) {
1132 data = 0x001;
1133 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1134 0x08, &data);
1135 } else {
1136 data = 0x002;
1137 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1138 0x08, &data);
1139 }
1140 } else {
1141 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1142 }
1143
1144 data = mos7720_port->shadowMCR;
1145 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1146
1147 /* Determine divisor based on baud rate */
1148 baud = tty_get_baud_rate(tty);
1149 if (!baud) {
1150 /* pick a default, any default... */
1151 dbg("Picked default baud...");
1152 baud = 9600;
1153 }
1154
1155 if (baud >= 230400) {
1156 set_higher_rates(mos7720_port, baud);
1157 /* Enable Interrupts */
1158 data = 0x0c;
1159 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1160 return;
1161 }
1162
Harvey Harrison441b62c2008-03-03 16:08:34 -08001163 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001164 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001165 /* FIXME: needs to write actual resulting baud back not just
1166 blindly do so */
1167 if (cflag & CBAUD)
1168 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001169 /* Enable Interrupts */
1170 data = 0x0c;
1171 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1172
1173 if (port->read_urb->status != -EINPROGRESS) {
1174 port->read_urb->dev = serial->dev;
1175
1176 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1177 if (status)
1178 dbg("usb_submit_urb(read bulk) failed, status = %d",
1179 status);
1180 }
1181 return;
1182}
1183
1184/*
1185 * mos7720_set_termios
1186 * this function is called by the tty driver when it wants to change the
1187 * termios structure.
1188 */
Alan Cox95da3102008-07-22 11:09:07 +01001189static void mos7720_set_termios(struct tty_struct *tty,
1190 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001191{
1192 int status;
1193 unsigned int cflag;
1194 struct usb_serial *serial;
1195 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001196
1197 serial = port->serial;
1198
1199 mos7720_port = usb_get_serial_port_data(port);
1200
1201 if (mos7720_port == NULL)
1202 return;
1203
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001204 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001205 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206 return;
1207 }
1208
Alan Cox4da1a172008-07-22 11:16:21 +01001209 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001210
1211 cflag = tty->termios->c_cflag;
1212
Harvey Harrison441b62c2008-03-03 16:08:34 -08001213 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214 tty->termios->c_cflag,
1215 RELEVANT_IFLAG(tty->termios->c_iflag));
1216
Harvey Harrison441b62c2008-03-03 16:08:34 -08001217 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063ab2008-01-03 17:01:18 +00001218 old_termios->c_cflag,
1219 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001220
Harvey Harrison441b62c2008-03-03 16:08:34 -08001221 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001222
1223 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001224 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001225
Alan Cox4da1a172008-07-22 11:16:21 +01001226 if (!port->read_urb) {
1227 dbg("%s", "URB KILLED !!!!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001228 return;
1229 }
1230
Alan Cox4da1a172008-07-22 11:16:21 +01001231 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232 port->read_urb->dev = serial->dev;
1233 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1234 if (status)
1235 dbg("usb_submit_urb(read bulk) failed, status = %d",
1236 status);
1237 }
1238 return;
1239}
1240
1241/*
1242 * get_lsr_info - get line status register info
1243 *
1244 * Purpose: Let user call ioctl() to get info when the UART physically
1245 * is emptied. On bus types like RS485, the transmitter must
1246 * release the bus after transmitting. This must be done when
1247 * the transmit shift register is empty, not be done when the
1248 * transmit holding register is empty. This functionality
1249 * allows an RS485 driver to be written in user space.
1250 */
Alan Cox4da1a172008-07-22 11:16:21 +01001251static int get_lsr_info(struct tty_struct *tty,
1252 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001253{
1254 int count;
1255 unsigned int result = 0;
1256
Alan Cox95da3102008-07-22 11:09:07 +01001257 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001258 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001259 dbg("%s -- Empty", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001260 result = TIOCSER_TEMT;
1261 }
1262
1263 if (copy_to_user(value, &result, sizeof(int)))
1264 return -EFAULT;
1265 return 0;
1266}
1267
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001268static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1269 unsigned int __user *value)
1270{
1271 unsigned int mcr ;
1272 unsigned int arg;
1273 unsigned char data;
1274
1275 struct usb_serial_port *port;
1276
1277 if (mos7720_port == NULL)
1278 return -1;
1279
Alan Cox4da1a172008-07-22 11:16:21 +01001280 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001281 mcr = mos7720_port->shadowMCR;
1282
1283 if (copy_from_user(&arg, value, sizeof(int)))
1284 return -EFAULT;
1285
1286 switch (cmd) {
1287 case TIOCMBIS:
1288 if (arg & TIOCM_RTS)
1289 mcr |= UART_MCR_RTS;
1290 if (arg & TIOCM_DTR)
1291 mcr |= UART_MCR_RTS;
1292 if (arg & TIOCM_LOOP)
1293 mcr |= UART_MCR_LOOP;
1294 break;
1295
1296 case TIOCMBIC:
1297 if (arg & TIOCM_RTS)
1298 mcr &= ~UART_MCR_RTS;
1299 if (arg & TIOCM_DTR)
1300 mcr &= ~UART_MCR_RTS;
1301 if (arg & TIOCM_LOOP)
1302 mcr &= ~UART_MCR_LOOP;
1303 break;
1304
1305 case TIOCMSET:
1306 /* turn off the RTS and DTR and LOOPBACK
1307 * and then only turn on what was asked to */
1308 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1309 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1310 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1311 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1312 break;
1313 }
1314
1315 mos7720_port->shadowMCR = mcr;
1316
1317 data = mos7720_port->shadowMCR;
1318 send_mos_cmd(port->serial, MOS_WRITE,
1319 port->number - port->serial->minor, UART_MCR, &data);
1320
1321 return 0;
1322}
1323
1324static int get_modem_info(struct moschip_port *mos7720_port,
1325 unsigned int __user *value)
1326{
1327 unsigned int result = 0;
1328 unsigned int msr = mos7720_port->shadowMSR;
1329 unsigned int mcr = mos7720_port->shadowMCR;
1330
1331 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1332 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1333 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1334 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1335 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1336 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1337
1338
Harvey Harrison441b62c2008-03-03 16:08:34 -08001339 dbg("%s -- %x", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001340
1341 if (copy_to_user(value, &result, sizeof(int)))
1342 return -EFAULT;
1343 return 0;
1344}
1345
1346static int get_serial_info(struct moschip_port *mos7720_port,
1347 struct serial_struct __user *retinfo)
1348{
1349 struct serial_struct tmp;
1350
1351 if (!retinfo)
1352 return -EFAULT;
1353
1354 memset(&tmp, 0, sizeof(tmp));
1355
1356 tmp.type = PORT_16550A;
1357 tmp.line = mos7720_port->port->serial->minor;
1358 tmp.port = mos7720_port->port->number;
1359 tmp.irq = 0;
1360 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001361 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001362 tmp.baud_base = 9600;
1363 tmp.close_delay = 5*HZ;
1364 tmp.closing_wait = 30*HZ;
1365
1366 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1367 return -EFAULT;
1368 return 0;
1369}
1370
Alan Cox95da3102008-07-22 11:09:07 +01001371static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001372 unsigned int cmd, unsigned long arg)
1373{
Alan Cox95da3102008-07-22 11:09:07 +01001374 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001375 struct moschip_port *mos7720_port;
1376 struct async_icount cnow;
1377 struct async_icount cprev;
1378 struct serial_icounter_struct icount;
1379
1380 mos7720_port = usb_get_serial_port_data(port);
1381 if (mos7720_port == NULL)
1382 return -ENODEV;
1383
Harvey Harrison441b62c2008-03-03 16:08:34 -08001384 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001385
1386 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001387 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001388 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01001389 return get_lsr_info(tty, mos7720_port,
1390 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001391 return 0;
1392
Alan Cox95da3102008-07-22 11:09:07 +01001393 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001394 case TIOCMBIS:
1395 case TIOCMBIC:
1396 case TIOCMSET:
Alan Cox4da1a172008-07-22 11:16:21 +01001397 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1398 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001399 return set_modem_info(mos7720_port, cmd,
1400 (unsigned int __user *)arg);
1401
1402 case TIOCMGET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 dbg("%s (%d) TIOCMGET", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001404 return get_modem_info(mos7720_port,
1405 (unsigned int __user *)arg);
1406
1407 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001408 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001409 return get_serial_info(mos7720_port,
1410 (struct serial_struct __user *)arg);
1411
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001412 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001413 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001414 cprev = mos7720_port->icount;
1415 while (1) {
1416 if (signal_pending(current))
1417 return -ERESTARTSYS;
1418 cnow = mos7720_port->icount;
1419 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1420 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1421 return -EIO; /* no change => error */
1422 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1423 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1424 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001425 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001426 return 0;
1427 }
1428 cprev = cnow;
1429 }
1430 /* NOTREACHED */
1431 break;
1432
1433 case TIOCGICOUNT:
1434 cnow = mos7720_port->icount;
1435 icount.cts = cnow.cts;
1436 icount.dsr = cnow.dsr;
1437 icount.rng = cnow.rng;
1438 icount.dcd = cnow.dcd;
1439 icount.rx = cnow.rx;
1440 icount.tx = cnow.tx;
1441 icount.frame = cnow.frame;
1442 icount.overrun = cnow.overrun;
1443 icount.parity = cnow.parity;
1444 icount.brk = cnow.brk;
1445 icount.buf_overrun = cnow.buf_overrun;
1446
Harvey Harrison441b62c2008-03-03 16:08:34 -08001447 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Alan Cox4da1a172008-07-22 11:16:21 +01001448 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001449 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1450 return -EFAULT;
1451 return 0;
1452 }
1453
1454 return -ENOIOCTLCMD;
1455}
1456
1457static int mos7720_startup(struct usb_serial *serial)
1458{
1459 struct moschip_serial *mos7720_serial;
1460 struct moschip_port *mos7720_port;
1461 struct usb_device *dev;
1462 int i;
1463 char data;
1464
Harvey Harrison441b62c2008-03-03 16:08:34 -08001465 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001466
1467 if (!serial) {
1468 dbg("Invalid Handler");
1469 return -ENODEV;
1470 }
1471
1472 dev = serial->dev;
1473
1474 /* create our private serial structure */
1475 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1476 if (mos7720_serial == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001477 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001478 return -ENOMEM;
1479 }
1480
1481 usb_set_serial_data(serial, mos7720_serial);
1482
1483 /* we set up the pointers to the endpoints in the mos7720_open *
1484 * function, as the structures aren't created yet. */
1485
1486 /* set up port private structures */
1487 for (i = 0; i < serial->num_ports; ++i) {
1488 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1489 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001490 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001491 usb_set_serial_data(serial, NULL);
1492 kfree(mos7720_serial);
1493 return -ENOMEM;
1494 }
1495
1496 /* Initialize all port interrupt end point to port 0 int
1497 * endpoint. Our device has only one interrupt endpoint
1498 * comman to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01001499 serial->port[i]->interrupt_in_endpointAddress =
1500 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001501
1502 mos7720_port->port = serial->port[i];
1503 usb_set_serial_port_data(serial->port[i], mos7720_port);
1504
1505 dbg("port number is %d", serial->port[i]->number);
1506 dbg("serial number is %d", serial->minor);
1507 }
1508
1509
1510 /* setting configuration feature to one */
1511 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001512 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001513
Alan Cox4da1a172008-07-22 11:16:21 +01001514 /* LSR For Port 1 */
1515 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1516 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001517
Alan Cox4da1a172008-07-22 11:16:21 +01001518 /* LSR For Port 2 */
1519 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1520 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001521
1522 return 0;
1523}
1524
1525static void mos7720_shutdown(struct usb_serial *serial)
1526{
1527 int i;
1528
1529 /* free private structure allocated for serial port */
Alan Cox4da1a172008-07-22 11:16:21 +01001530 for (i = 0; i < serial->num_ports; ++i) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001531 kfree(usb_get_serial_port_data(serial->port[i]));
1532 usb_set_serial_port_data(serial->port[i], NULL);
1533 }
1534
1535 /* free private structure allocated for serial device */
1536 kfree(usb_get_serial_data(serial));
1537 usb_set_serial_data(serial, NULL);
1538}
1539
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001540static struct usb_driver usb_driver = {
1541 .name = "moschip7720",
1542 .probe = usb_serial_probe,
1543 .disconnect = usb_serial_disconnect,
1544 .id_table = moschip_port_id_table,
1545 .no_dynamic_id = 1,
1546};
1547
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548static struct usb_serial_driver moschip7720_2port_driver = {
1549 .driver = {
1550 .owner = THIS_MODULE,
1551 .name = "moschip7720",
1552 },
1553 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001554 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001555 .id_table = moschip_port_id_table,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001556 .num_ports = 2,
1557 .open = mos7720_open,
1558 .close = mos7720_close,
1559 .throttle = mos7720_throttle,
1560 .unthrottle = mos7720_unthrottle,
1561 .attach = mos7720_startup,
1562 .shutdown = mos7720_shutdown,
1563 .ioctl = mos7720_ioctl,
1564 .set_termios = mos7720_set_termios,
1565 .write = mos7720_write,
1566 .write_room = mos7720_write_room,
1567 .chars_in_buffer = mos7720_chars_in_buffer,
1568 .break_ctl = mos7720_break,
1569 .read_bulk_callback = mos7720_bulk_in_callback,
Oliver Neukume8e30c72007-03-14 11:11:08 +01001570 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001571};
1572
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001573static int __init moschip7720_init(void)
1574{
1575 int retval;
1576
Harvey Harrison441b62c2008-03-03 16:08:34 -08001577 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001578
1579 /* Register with the usb serial */
1580 retval = usb_serial_register(&moschip7720_2port_driver);
1581 if (retval)
1582 goto failed_port_device_register;
1583
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001584 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1585 DRIVER_DESC "\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001586
1587 /* Register with the usb */
1588 retval = usb_register(&usb_driver);
1589 if (retval)
1590 goto failed_usb_register;
1591
1592 return 0;
1593
1594failed_usb_register:
1595 usb_serial_deregister(&moschip7720_2port_driver);
1596
1597failed_port_device_register:
1598 return retval;
1599}
1600
1601static void __exit moschip7720_exit(void)
1602{
1603 usb_deregister(&usb_driver);
1604 usb_serial_deregister(&moschip7720_2port_driver);
1605}
1606
1607module_init(moschip7720_init);
1608module_exit(moschip7720_exit);
1609
1610/* Module information */
Alan Cox4da1a172008-07-22 11:16:21 +01001611MODULE_AUTHOR(DRIVER_AUTHOR);
1612MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001613MODULE_LICENSE("GPL");
1614
1615module_param(debug, bool, S_IRUGO | S_IWUSR);
1616MODULE_PARM_DESC(debug, "Debug enabled or not");