blob: 5711aa55d24b4e6446d37a3b4debaa785663d6bb [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 Neukumfe4b65ec92007-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) {
358 err("No more urbs???");
359 continue;
360 }
361
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 GFP_KERNEL);
364 if (!urb->transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 err("%s-out of memory for urb buffers.", __func__);
Oliver Neukumfe4b65ec92007-03-14 15:22:25 +0100366 usb_free_urb(mos7720_port->write_urb_pool[j]);
367 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700368 continue;
369 }
Oliver Neukumfe4b65ec92007-03-14 15:22:25 +0100370 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700371 }
372
Oliver Neukumfe4b65ec92007-03-14 15:22:25 +0100373 if (!allocated_urbs)
374 return -ENOMEM;
375
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700376 /* Initialize MCS7720 -- Write Init values to corresponding Registers
377 *
378 * Register Index
379 * 1 : IER
380 * 2 : FCR
381 * 3 : LCR
382 * 4 : MCR
383 *
384 * 0x08 : SP1/2 Control Reg
385 */
386 port_number = port->number - port->serial->minor;
387 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +0100388 dbg("SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700389
390 dbg("Check:Sending Command ..........");
391
392 data = 0x02;
393 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
394 data = 0x02;
395 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
396
397 data = 0x00;
398 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
399 data = 0x00;
400 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
401
402 data = 0xCF;
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
404 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100405 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700406 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
407 data = 0x0b;
Alan Cox4da1a172008-07-22 11:16:21 +0100408 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700409 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
410 data = 0x0b;
411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
412
413 data = 0x00;
414 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
415 data = 0x00;
416 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
417
418/* data = 0x00;
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
420 data = 0x03;
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
422 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100423 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
424 port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700425*/
426 data = 0x00;
427 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
428
429 data = data | (port->number - port->serial->minor + 1);
430 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
431
432 data = 0x83;
Alan Cox4da1a172008-07-22 11:16:21 +0100433 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700434 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
435 data = 0x0c;
436 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
437 data = 0x00;
438 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
439 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100440 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700441 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
442 data = 0x0c;
443 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
444 data = 0x0c;
445 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
446
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700447 /* force low_latency on so that our tty_push actually forces *
448 * the data through,otherwise it is scheduled, and with *
449 * high data rates (like with OHCI) data can get lost. */
450
Alan Cox95da3102008-07-22 11:09:07 +0100451 if (tty)
452 tty->low_latency = 1;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700453
454 /* see if we've set up our endpoint info yet *
455 * (can't set it up in mos7720_startup as the *
456 * structures were not set up at that time.) */
457 if (!mos7720_serial->interrupt_started) {
458 dbg("Interrupt buffer NULL !!!");
459
460 /* not set up yet, so do it now */
461 mos7720_serial->interrupt_started = 1;
462
463 dbg("To Submit URB !!!");
464
465 /* set up our interrupt urb */
466 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100467 usb_rcvintpipe(serial->dev,
468 port->interrupt_in_endpointAddress),
469 port0->interrupt_in_buffer,
470 port0->interrupt_in_urb->transfer_buffer_length,
471 mos7720_interrupt_callback, mos7720_port,
472 port0->interrupt_in_urb->interval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700473
474 /* start interrupt read for this mos7720 this interrupt *
Alan Cox4da1a172008-07-22 11:16:21 +0100475 * will continue as long as the mos7720 is connected */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700476 dbg("Submit URB over !!!");
477 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
478 if (response)
479 dev_err(&port->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700480 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800481 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700482 }
483
484 /* set up our bulk in urb */
485 usb_fill_bulk_urb(port->read_urb, serial->dev,
486 usb_rcvbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100487 port->bulk_in_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700488 port->bulk_in_buffer,
489 port->read_urb->transfer_buffer_length,
490 mos7720_bulk_in_callback, mos7720_port);
491 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
492 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100493 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
494 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700495
496 /* initialize our icount structure */
497 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
498
499 /* initialize our port settings */
500 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
501
502 /* send a open port command */
503 mos7720_port->open = 1;
504
505 return 0;
506}
507
508/*
509 * mos7720_chars_in_buffer
510 * this function is called by the tty driver when it wants to know how many
511 * bytes of data we currently have outstanding in the port (data that has
512 * been written, but hasn't made it out the port yet)
513 * If successful, we return the number of bytes left to be written in the
514 * system,
515 * Otherwise we return a negative error number.
516 */
Alan Cox95da3102008-07-22 11:09:07 +0100517static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700518{
Alan Cox95da3102008-07-22 11:09:07 +0100519 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700520 int i;
521 int chars = 0;
522 struct moschip_port *mos7720_port;
523
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700525
526 mos7720_port = usb_get_serial_port_data(port);
527 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800528 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700529 return -ENODEV;
530 }
531
532 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100533 if (mos7720_port->write_urb_pool[i] &&
534 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700535 chars += URB_TRANSFER_BUFFER_SIZE;
536 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800537 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700538 return chars;
539}
540
Alan Cox95da3102008-07-22 11:09:07 +0100541static void mos7720_close(struct tty_struct *tty,
542 struct usb_serial_port *port, struct file *filp)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700543{
544 struct usb_serial *serial;
545 struct moschip_port *mos7720_port;
546 char data;
547 int j;
548
549 dbg("mos7720_close:entering...");
550
551 serial = port->serial;
552
553 mos7720_port = usb_get_serial_port_data(port);
554 if (mos7720_port == NULL)
555 return;
556
557 for (j = 0; j < NUM_URBS; ++j)
558 usb_kill_urb(mos7720_port->write_urb_pool[j]);
559
560 /* Freeing Write URBs */
561 for (j = 0; j < NUM_URBS; ++j) {
562 if (mos7720_port->write_urb_pool[j]) {
563 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
564 usb_free_urb(mos7720_port->write_urb_pool[j]);
565 }
566 }
567
568 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100569 * and interrupt read if they exists, otherwise nop */
570 dbg("Shutdown bulk write");
571 usb_kill_urb(port->write_urb);
572 dbg("Shutdown bulk read");
573 usb_kill_urb(port->read_urb);
574
575 mutex_lock(&serial->disc_mutex);
576 /* these commands must not be issued if the device has
577 * been disconnected */
578 if (!serial->disconnected) {
579 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100580 send_mos_cmd(serial, MOS_WRITE,
581 port->number - port->serial->minor, 0x04, &data);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100582
583 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100584 send_mos_cmd(serial, MOS_WRITE,
585 port->number - port->serial->minor, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700586 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100587 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700588 mos7720_port->open = 0;
589
Harvey Harrison441b62c2008-03-03 16:08:34 -0800590 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700591}
592
Alan Cox95da3102008-07-22 11:09:07 +0100593static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700594{
Alan Cox95da3102008-07-22 11:09:07 +0100595 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +0100596 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700597 struct usb_serial *serial;
598 struct moschip_port *mos7720_port;
599
Harvey Harrison441b62c2008-03-03 16:08:34 -0800600 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700601
602 serial = port->serial;
603
604 mos7720_port = usb_get_serial_port_data(port);
605 if (mos7720_port == NULL)
606 return;
607
608 if (break_state == -1)
609 data = mos7720_port->shadowLCR | UART_LCR_SBC;
610 else
611 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
612
613 mos7720_port->shadowLCR = data;
614 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
615 0x03, &data);
616
617 return;
618}
619
620/*
621 * mos7720_write_room
622 * this function is called by the tty driver when it wants to know how many
623 * bytes of data we can accept for a specific port.
624 * If successful, we return the amount of room that we have for this port
625 * Otherwise we return a negative error number.
626 */
Alan Cox95da3102008-07-22 11:09:07 +0100627static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700628{
Alan Cox95da3102008-07-22 11:09:07 +0100629 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700630 struct moschip_port *mos7720_port;
631 int room = 0;
632 int i;
633
Harvey Harrison441b62c2008-03-03 16:08:34 -0800634 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700635
636 mos7720_port = usb_get_serial_port_data(port);
637 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800638 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700639 return -ENODEV;
640 }
641
Alan Coxa5b6f602008-04-08 17:16:06 +0100642 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700643 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100644 if (mos7720_port->write_urb_pool[i] &&
645 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700646 room += URB_TRANSFER_BUFFER_SIZE;
647 }
648
Harvey Harrison441b62c2008-03-03 16:08:34 -0800649 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700650 return room;
651}
652
Alan Cox95da3102008-07-22 11:09:07 +0100653static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
654 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700655{
656 int status;
657 int i;
658 int bytes_sent = 0;
659 int transfer_size;
660
661 struct moschip_port *mos7720_port;
662 struct usb_serial *serial;
663 struct urb *urb;
664 const unsigned char *current_position = data;
665
Harvey Harrison441b62c2008-03-03 16:08:34 -0800666 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700667
668 serial = port->serial;
669
670 mos7720_port = usb_get_serial_port_data(port);
671 if (mos7720_port == NULL) {
672 dbg("mos7720_port is NULL");
673 return -ENODEV;
674 }
675
676 /* try to find a free urb in the list */
677 urb = NULL;
678
679 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100680 if (mos7720_port->write_urb_pool[i] &&
681 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700682 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +0100683 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700684 break;
685 }
686 }
687
688 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800689 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700690 goto exit;
691 }
692
693 if (urb->transfer_buffer == NULL) {
694 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
695 GFP_KERNEL);
696 if (urb->transfer_buffer == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800697 err("%s no more kernel memory...", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700698 goto exit;
699 }
700 }
Alan Cox4da1a172008-07-22 11:16:21 +0100701 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700702
703 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800704 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700705 urb->transfer_buffer);
706
707 /* fill urb with data and submit */
708 usb_fill_bulk_urb(urb, serial->dev,
709 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100710 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700711 urb->transfer_buffer, transfer_size,
712 mos7720_bulk_out_data_callback, mos7720_port);
713
714 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +0100715 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700716 if (status) {
717 err("%s - usb_submit_urb(write bulk) failed with status = %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800718 __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700719 bytes_sent = status;
720 goto exit;
721 }
722 bytes_sent = transfer_size;
723
724exit:
725 return bytes_sent;
726}
727
Alan Cox95da3102008-07-22 11:09:07 +0100728static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700729{
Alan Cox95da3102008-07-22 11:09:07 +0100730 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700731 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700732 int status;
733
Harvey Harrison441b62c2008-03-03 16:08:34 -0800734 dbg("%s- port %d\n", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700735
736 mos7720_port = usb_get_serial_port_data(port);
737
738 if (mos7720_port == NULL)
739 return;
740
741 if (!mos7720_port->open) {
742 dbg("port not opened");
743 return;
744 }
745
Harvey Harrison441b62c2008-03-03 16:08:34 -0800746 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700747
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748 /* if we are implementing XON/XOFF, send the stop character */
749 if (I_IXOFF(tty)) {
750 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100751 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700752 if (status <= 0)
753 return;
754 }
755
756 /* if we are implementing RTS/CTS, toggle that line */
757 if (tty->termios->c_cflag & CRTSCTS) {
758 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
759 status = send_mos_cmd(port->serial, MOS_WRITE,
760 port->number - port->serial->minor,
761 UART_MCR, &mos7720_port->shadowMCR);
762 if (status != 0)
763 return;
764 }
765}
766
Alan Cox95da3102008-07-22 11:09:07 +0100767static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700768{
Alan Cox95da3102008-07-22 11:09:07 +0100769 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700770 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100771 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700772
773 if (mos7720_port == NULL)
774 return;
775
776 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800777 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700778 return;
779 }
780
Harvey Harrison441b62c2008-03-03 16:08:34 -0800781 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700782
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700783 /* if we are implementing XON/XOFF, send the start character */
784 if (I_IXOFF(tty)) {
785 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100786 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700787 if (status <= 0)
788 return;
789 }
790
791 /* if we are implementing RTS/CTS, toggle that line */
792 if (tty->termios->c_cflag & CRTSCTS) {
793 mos7720_port->shadowMCR |= UART_MCR_RTS;
794 status = send_mos_cmd(port->serial, MOS_WRITE,
795 port->number - port->serial->minor,
796 UART_MCR, &mos7720_port->shadowMCR);
797 if (status != 0)
798 return;
799 }
800}
801
802static int set_higher_rates(struct moschip_port *mos7720_port,
803 unsigned int baud)
804{
805 unsigned char data;
806 struct usb_serial_port *port;
807 struct usb_serial *serial;
808 int port_number;
809
810 if (mos7720_port == NULL)
811 return -EINVAL;
812
813 port = mos7720_port->port;
814 serial = port->serial;
815
Alan Cox4da1a172008-07-22 11:16:21 +0100816 /***********************************************
817 * Init Sequence for higher rates
818 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700819 dbg("Sending Setting Commands ..........");
820 port_number = port->number - port->serial->minor;
821
822 data = 0x000;
823 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
824 data = 0x000;
825 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
826 data = 0x0CF;
827 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
828 data = 0x00b;
Alan Cox4da1a172008-07-22 11:16:21 +0100829 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
831 data = 0x00b;
832 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
833
834 data = 0x000;
835 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
836 data = 0x000;
837 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
838
839
Alan Cox4da1a172008-07-22 11:16:21 +0100840 /***********************************************
841 * Set for higher rates *
842 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700843
844 data = baud * 0x10;
Alan Cox4da1a172008-07-22 11:16:21 +0100845 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700846
847 data = 0x003;
848 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
849 data = 0x003;
850 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
851
852 data = 0x02b;
Alan Cox4da1a172008-07-22 11:16:21 +0100853 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700854 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
855 data = 0x02b;
856 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
857
Alan Cox4da1a172008-07-22 11:16:21 +0100858 /***********************************************
859 * Set DLL/DLM
860 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700861
862 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100863 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700864 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
865
866 data = 0x001; /* DLL */
Alan Cox4da1a172008-07-22 11:16:21 +0100867 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700868 data = 0x000; /* DLM */
Alan Cox4da1a172008-07-22 11:16:21 +0100869 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700870
871 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100872 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700873 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
874
875 return 0;
876}
877
878/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +0100879struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700880 __u32 baudrate;
881 __u16 divisor;
882};
883
884/* Define table of divisors for moschip 7720 hardware *
885 * These assume a 3.6864MHz crystal, the standard /16, and *
886 * MCR.7 = 0. */
887static struct divisor_table_entry divisor_table[] = {
888 { 50, 2304},
889 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
890 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
891 { 150, 768},
892 { 300, 384},
893 { 600, 192},
894 { 1200, 96},
895 { 1800, 64},
896 { 2400, 48},
897 { 4800, 24},
898 { 7200, 16},
899 { 9600, 12},
900 { 19200, 6},
901 { 38400, 3},
902 { 57600, 2},
903 { 115200, 1},
904};
905
906/*****************************************************************************
907 * calc_baud_rate_divisor
908 * this function calculates the proper baud rate divisor for the specified
909 * baud rate.
910 *****************************************************************************/
911static int calc_baud_rate_divisor(int baudrate, int *divisor)
912{
913 int i;
914 __u16 custom;
915 __u16 round1;
916 __u16 round;
917
918
Harvey Harrison441b62c2008-03-03 16:08:34 -0800919 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700920
921 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
922 if (divisor_table[i].baudrate == baudrate) {
923 *divisor = divisor_table[i].divisor;
924 return 0;
925 }
926 }
927
Alan Cox4da1a172008-07-22 11:16:21 +0100928 /* After trying for all the standard baud rates *
929 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700930 if (baudrate > 75 && baudrate < 230400) {
931 /* get the divisor */
932 custom = (__u16)(230400L / baudrate);
933
934 /* Check for round off */
935 round1 = (__u16)(2304000L / baudrate);
936 round = (__u16)(round1 - (custom * 10));
937 if (round > 4)
938 custom++;
939 *divisor = custom;
940
Alan Cox4da1a172008-07-22 11:16:21 +0100941 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700942 return 0;
943 }
944
945 dbg("Baud calculation Failed...");
946 return -EINVAL;
947}
948
949/*
950 * send_cmd_write_baud_rate
951 * this function sends the proper command to change the baud rate of the
952 * specified port.
953 */
954static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
955 int baudrate)
956{
957 struct usb_serial_port *port;
958 struct usb_serial *serial;
959 int divisor;
960 int status;
961 unsigned char data;
962 unsigned char number;
963
964 if (mos7720_port == NULL)
965 return -1;
966
967 port = mos7720_port->port;
968 serial = port->serial;
969
Harvey Harrison441b62c2008-03-03 16:08:34 -0800970 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700971
972 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800973 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700974
Alan Cox4da1a172008-07-22 11:16:21 +0100975 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700976 status = calc_baud_rate_divisor(baudrate, &divisor);
977 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800978 err("%s - bad baud rate", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700979 return status;
980 }
981
Alan Cox4da1a172008-07-22 11:16:21 +0100982 /* Enable access to divisor latch */
983 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
984 mos7720_port->shadowLCR = data;
985 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700986
987 /* Write the divisor */
988 data = ((unsigned char)(divisor & 0xff));
Alan Cox4da1a172008-07-22 11:16:21 +0100989 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700990
991 data = ((unsigned char)((divisor & 0xff00) >> 8));
Alan Cox4da1a172008-07-22 11:16:21 +0100992 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700993
Alan Cox4da1a172008-07-22 11:16:21 +0100994 /* Disable access to divisor latch */
995 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
996 mos7720_port->shadowLCR = data;
997 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700998
999 return status;
1000}
1001
1002/*
1003 * change_port_settings
1004 * This routine is called to set the UART on the device to match
1005 * the specified new settings.
1006 */
Alan Cox95da3102008-07-22 11:09:07 +01001007static void change_port_settings(struct tty_struct *tty,
1008 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001009 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001010{
1011 struct usb_serial_port *port;
1012 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001013 int baud;
1014 unsigned cflag;
1015 unsigned iflag;
1016 __u8 mask = 0xff;
1017 __u8 lData;
1018 __u8 lParity;
1019 __u8 lStop;
1020 int status;
1021 int port_number;
1022 char data;
1023
1024 if (mos7720_port == NULL)
1025 return ;
1026
1027 port = mos7720_port->port;
1028 serial = port->serial;
1029 port_number = port->number - port->serial->minor;
1030
Harvey Harrison441b62c2008-03-03 16:08:34 -08001031 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001032
1033 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001034 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001035 return;
1036 }
1037
Harvey Harrison441b62c2008-03-03 16:08:34 -08001038 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001039
1040 lData = UART_LCR_WLEN8;
1041 lStop = 0x00; /* 1 stop bit */
1042 lParity = 0x00; /* No parity */
1043
1044 cflag = tty->termios->c_cflag;
1045 iflag = tty->termios->c_iflag;
1046
1047 /* Change the number of bits */
1048 switch (cflag & CSIZE) {
1049 case CS5:
1050 lData = UART_LCR_WLEN5;
1051 mask = 0x1f;
1052 break;
1053
1054 case CS6:
1055 lData = UART_LCR_WLEN6;
1056 mask = 0x3f;
1057 break;
1058
1059 case CS7:
1060 lData = UART_LCR_WLEN7;
1061 mask = 0x7f;
1062 break;
1063 default:
1064 case CS8:
1065 lData = UART_LCR_WLEN8;
1066 break;
1067 }
1068
1069 /* Change the Parity bit */
1070 if (cflag & PARENB) {
1071 if (cflag & PARODD) {
1072 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001074 } else {
1075 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001076 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001077 }
1078
1079 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001080 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001081 }
1082
1083 if (cflag & CMSPAR)
1084 lParity = lParity | 0x20;
1085
1086 /* Change the Stop bit */
1087 if (cflag & CSTOPB) {
1088 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001089 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001090 } else {
1091 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001092 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001093 }
1094
1095#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1096#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1097#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1098
1099 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001100 mos7720_port->shadowLCR &=
1101 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001102 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1103
1104
1105 /* Disable Interrupts */
1106 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001107 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1108 UART_IER, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109
1110 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001111 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001112
1113 data = 0xcf;
Alan Cox4da1a172008-07-22 11:16:21 +01001114 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001115
1116 /* Send the updated LCR value to the mos7720 */
1117 data = mos7720_port->shadowLCR;
Alan Cox4da1a172008-07-22 11:16:21 +01001118 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001119
Alan Cox4da1a172008-07-22 11:16:21 +01001120 data = 0x00b;
1121 mos7720_port->shadowMCR = data;
1122 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1123 data = 0x00b;
1124 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001125
1126 /* set up the MCR register and send it to the mos7720 */
1127 mos7720_port->shadowMCR = UART_MCR_OUT2;
1128 if (cflag & CBAUD)
1129 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1130
1131 if (cflag & CRTSCTS) {
1132 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001133 /* To set hardware flow control to the specified *
1134 * serial port, in SP1/2_CONTROL_REG */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001135 if (port->number) {
1136 data = 0x001;
1137 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1138 0x08, &data);
1139 } else {
1140 data = 0x002;
1141 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1142 0x08, &data);
1143 }
1144 } else {
1145 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1146 }
1147
1148 data = mos7720_port->shadowMCR;
1149 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1150
1151 /* Determine divisor based on baud rate */
1152 baud = tty_get_baud_rate(tty);
1153 if (!baud) {
1154 /* pick a default, any default... */
1155 dbg("Picked default baud...");
1156 baud = 9600;
1157 }
1158
1159 if (baud >= 230400) {
1160 set_higher_rates(mos7720_port, baud);
1161 /* Enable Interrupts */
1162 data = 0x0c;
1163 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1164 return;
1165 }
1166
Harvey Harrison441b62c2008-03-03 16:08:34 -08001167 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001168 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001169 /* FIXME: needs to write actual resulting baud back not just
1170 blindly do so */
1171 if (cflag & CBAUD)
1172 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001173 /* Enable Interrupts */
1174 data = 0x0c;
1175 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1176
1177 if (port->read_urb->status != -EINPROGRESS) {
1178 port->read_urb->dev = serial->dev;
1179
1180 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1181 if (status)
1182 dbg("usb_submit_urb(read bulk) failed, status = %d",
1183 status);
1184 }
1185 return;
1186}
1187
1188/*
1189 * mos7720_set_termios
1190 * this function is called by the tty driver when it wants to change the
1191 * termios structure.
1192 */
Alan Cox95da3102008-07-22 11:09:07 +01001193static void mos7720_set_termios(struct tty_struct *tty,
1194 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001195{
1196 int status;
1197 unsigned int cflag;
1198 struct usb_serial *serial;
1199 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001200
1201 serial = port->serial;
1202
1203 mos7720_port = usb_get_serial_port_data(port);
1204
1205 if (mos7720_port == NULL)
1206 return;
1207
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001209 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001210 return;
1211 }
1212
Alan Cox4da1a172008-07-22 11:16:21 +01001213 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214
1215 cflag = tty->termios->c_cflag;
1216
Harvey Harrison441b62c2008-03-03 16:08:34 -08001217 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001218 tty->termios->c_cflag,
1219 RELEVANT_IFLAG(tty->termios->c_iflag));
1220
Harvey Harrison441b62c2008-03-03 16:08:34 -08001221 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063ab2008-01-03 17:01:18 +00001222 old_termios->c_cflag,
1223 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001224
Harvey Harrison441b62c2008-03-03 16:08:34 -08001225 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001226
1227 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001228 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001229
Alan Cox4da1a172008-07-22 11:16:21 +01001230 if (!port->read_urb) {
1231 dbg("%s", "URB KILLED !!!!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232 return;
1233 }
1234
Alan Cox4da1a172008-07-22 11:16:21 +01001235 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001236 port->read_urb->dev = serial->dev;
1237 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1238 if (status)
1239 dbg("usb_submit_urb(read bulk) failed, status = %d",
1240 status);
1241 }
1242 return;
1243}
1244
1245/*
1246 * get_lsr_info - get line status register info
1247 *
1248 * Purpose: Let user call ioctl() to get info when the UART physically
1249 * is emptied. On bus types like RS485, the transmitter must
1250 * release the bus after transmitting. This must be done when
1251 * the transmit shift register is empty, not be done when the
1252 * transmit holding register is empty. This functionality
1253 * allows an RS485 driver to be written in user space.
1254 */
Alan Cox4da1a172008-07-22 11:16:21 +01001255static int get_lsr_info(struct tty_struct *tty,
1256 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257{
1258 int count;
1259 unsigned int result = 0;
1260
Alan Cox95da3102008-07-22 11:09:07 +01001261 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001262 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001263 dbg("%s -- Empty", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 result = TIOCSER_TEMT;
1265 }
1266
1267 if (copy_to_user(value, &result, sizeof(int)))
1268 return -EFAULT;
1269 return 0;
1270}
1271
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001272static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1273 unsigned int __user *value)
1274{
1275 unsigned int mcr ;
1276 unsigned int arg;
1277 unsigned char data;
1278
1279 struct usb_serial_port *port;
1280
1281 if (mos7720_port == NULL)
1282 return -1;
1283
Alan Cox4da1a172008-07-22 11:16:21 +01001284 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001285 mcr = mos7720_port->shadowMCR;
1286
1287 if (copy_from_user(&arg, value, sizeof(int)))
1288 return -EFAULT;
1289
1290 switch (cmd) {
1291 case TIOCMBIS:
1292 if (arg & TIOCM_RTS)
1293 mcr |= UART_MCR_RTS;
1294 if (arg & TIOCM_DTR)
1295 mcr |= UART_MCR_RTS;
1296 if (arg & TIOCM_LOOP)
1297 mcr |= UART_MCR_LOOP;
1298 break;
1299
1300 case TIOCMBIC:
1301 if (arg & TIOCM_RTS)
1302 mcr &= ~UART_MCR_RTS;
1303 if (arg & TIOCM_DTR)
1304 mcr &= ~UART_MCR_RTS;
1305 if (arg & TIOCM_LOOP)
1306 mcr &= ~UART_MCR_LOOP;
1307 break;
1308
1309 case TIOCMSET:
1310 /* turn off the RTS and DTR and LOOPBACK
1311 * and then only turn on what was asked to */
1312 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1313 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1314 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1315 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1316 break;
1317 }
1318
1319 mos7720_port->shadowMCR = mcr;
1320
1321 data = mos7720_port->shadowMCR;
1322 send_mos_cmd(port->serial, MOS_WRITE,
1323 port->number - port->serial->minor, UART_MCR, &data);
1324
1325 return 0;
1326}
1327
1328static int get_modem_info(struct moschip_port *mos7720_port,
1329 unsigned int __user *value)
1330{
1331 unsigned int result = 0;
1332 unsigned int msr = mos7720_port->shadowMSR;
1333 unsigned int mcr = mos7720_port->shadowMCR;
1334
1335 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1336 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1337 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1338 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1339 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1340 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1341
1342
Harvey Harrison441b62c2008-03-03 16:08:34 -08001343 dbg("%s -- %x", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001344
1345 if (copy_to_user(value, &result, sizeof(int)))
1346 return -EFAULT;
1347 return 0;
1348}
1349
1350static int get_serial_info(struct moschip_port *mos7720_port,
1351 struct serial_struct __user *retinfo)
1352{
1353 struct serial_struct tmp;
1354
1355 if (!retinfo)
1356 return -EFAULT;
1357
1358 memset(&tmp, 0, sizeof(tmp));
1359
1360 tmp.type = PORT_16550A;
1361 tmp.line = mos7720_port->port->serial->minor;
1362 tmp.port = mos7720_port->port->number;
1363 tmp.irq = 0;
1364 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001365 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001366 tmp.baud_base = 9600;
1367 tmp.close_delay = 5*HZ;
1368 tmp.closing_wait = 30*HZ;
1369
1370 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1371 return -EFAULT;
1372 return 0;
1373}
1374
Alan Cox95da3102008-07-22 11:09:07 +01001375static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001376 unsigned int cmd, unsigned long arg)
1377{
Alan Cox95da3102008-07-22 11:09:07 +01001378 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001379 struct moschip_port *mos7720_port;
1380 struct async_icount cnow;
1381 struct async_icount cprev;
1382 struct serial_icounter_struct icount;
1383
1384 mos7720_port = usb_get_serial_port_data(port);
1385 if (mos7720_port == NULL)
1386 return -ENODEV;
1387
Harvey Harrison441b62c2008-03-03 16:08:34 -08001388 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001389
1390 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001391 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001392 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01001393 return get_lsr_info(tty, mos7720_port,
1394 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001395 return 0;
1396
Alan Cox95da3102008-07-22 11:09:07 +01001397 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001398 case TIOCMBIS:
1399 case TIOCMBIC:
1400 case TIOCMSET:
Alan Cox4da1a172008-07-22 11:16:21 +01001401 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1402 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001403 return set_modem_info(mos7720_port, cmd,
1404 (unsigned int __user *)arg);
1405
1406 case TIOCMGET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001407 dbg("%s (%d) TIOCMGET", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001408 return get_modem_info(mos7720_port,
1409 (unsigned int __user *)arg);
1410
1411 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001412 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001413 return get_serial_info(mos7720_port,
1414 (struct serial_struct __user *)arg);
1415
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001416 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001417 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001418 cprev = mos7720_port->icount;
1419 while (1) {
1420 if (signal_pending(current))
1421 return -ERESTARTSYS;
1422 cnow = mos7720_port->icount;
1423 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1424 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1425 return -EIO; /* no change => error */
1426 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1427 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1428 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001429 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001430 return 0;
1431 }
1432 cprev = cnow;
1433 }
1434 /* NOTREACHED */
1435 break;
1436
1437 case TIOCGICOUNT:
1438 cnow = mos7720_port->icount;
1439 icount.cts = cnow.cts;
1440 icount.dsr = cnow.dsr;
1441 icount.rng = cnow.rng;
1442 icount.dcd = cnow.dcd;
1443 icount.rx = cnow.rx;
1444 icount.tx = cnow.tx;
1445 icount.frame = cnow.frame;
1446 icount.overrun = cnow.overrun;
1447 icount.parity = cnow.parity;
1448 icount.brk = cnow.brk;
1449 icount.buf_overrun = cnow.buf_overrun;
1450
Harvey Harrison441b62c2008-03-03 16:08:34 -08001451 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Alan Cox4da1a172008-07-22 11:16:21 +01001452 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001453 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1454 return -EFAULT;
1455 return 0;
1456 }
1457
1458 return -ENOIOCTLCMD;
1459}
1460
1461static int mos7720_startup(struct usb_serial *serial)
1462{
1463 struct moschip_serial *mos7720_serial;
1464 struct moschip_port *mos7720_port;
1465 struct usb_device *dev;
1466 int i;
1467 char data;
1468
Harvey Harrison441b62c2008-03-03 16:08:34 -08001469 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001470
1471 if (!serial) {
1472 dbg("Invalid Handler");
1473 return -ENODEV;
1474 }
1475
1476 dev = serial->dev;
1477
1478 /* create our private serial structure */
1479 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1480 if (mos7720_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001481 err("%s - Out of memory", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001482 return -ENOMEM;
1483 }
1484
1485 usb_set_serial_data(serial, mos7720_serial);
1486
1487 /* we set up the pointers to the endpoints in the mos7720_open *
1488 * function, as the structures aren't created yet. */
1489
1490 /* set up port private structures */
1491 for (i = 0; i < serial->num_ports; ++i) {
1492 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1493 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001494 err("%s - Out of memory", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001495 usb_set_serial_data(serial, NULL);
1496 kfree(mos7720_serial);
1497 return -ENOMEM;
1498 }
1499
1500 /* Initialize all port interrupt end point to port 0 int
1501 * endpoint. Our device has only one interrupt endpoint
1502 * comman to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01001503 serial->port[i]->interrupt_in_endpointAddress =
1504 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001505
1506 mos7720_port->port = serial->port[i];
1507 usb_set_serial_port_data(serial->port[i], mos7720_port);
1508
1509 dbg("port number is %d", serial->port[i]->number);
1510 dbg("serial number is %d", serial->minor);
1511 }
1512
1513
1514 /* setting configuration feature to one */
1515 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001516 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001517
Alan Cox4da1a172008-07-22 11:16:21 +01001518 /* LSR For Port 1 */
1519 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1520 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001521
Alan Cox4da1a172008-07-22 11:16:21 +01001522 /* LSR For Port 2 */
1523 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1524 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001525
1526 return 0;
1527}
1528
1529static void mos7720_shutdown(struct usb_serial *serial)
1530{
1531 int i;
1532
1533 /* free private structure allocated for serial port */
Alan Cox4da1a172008-07-22 11:16:21 +01001534 for (i = 0; i < serial->num_ports; ++i) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001535 kfree(usb_get_serial_port_data(serial->port[i]));
1536 usb_set_serial_port_data(serial->port[i], NULL);
1537 }
1538
1539 /* free private structure allocated for serial device */
1540 kfree(usb_get_serial_data(serial));
1541 usb_set_serial_data(serial, NULL);
1542}
1543
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001544static struct usb_driver usb_driver = {
1545 .name = "moschip7720",
1546 .probe = usb_serial_probe,
1547 .disconnect = usb_serial_disconnect,
1548 .id_table = moschip_port_id_table,
1549 .no_dynamic_id = 1,
1550};
1551
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001552static struct usb_serial_driver moschip7720_2port_driver = {
1553 .driver = {
1554 .owner = THIS_MODULE,
1555 .name = "moschip7720",
1556 },
1557 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001558 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001559 .id_table = moschip_port_id_table,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001560 .num_ports = 2,
1561 .open = mos7720_open,
1562 .close = mos7720_close,
1563 .throttle = mos7720_throttle,
1564 .unthrottle = mos7720_unthrottle,
1565 .attach = mos7720_startup,
1566 .shutdown = mos7720_shutdown,
1567 .ioctl = mos7720_ioctl,
1568 .set_termios = mos7720_set_termios,
1569 .write = mos7720_write,
1570 .write_room = mos7720_write_room,
1571 .chars_in_buffer = mos7720_chars_in_buffer,
1572 .break_ctl = mos7720_break,
1573 .read_bulk_callback = mos7720_bulk_in_callback,
Oliver Neukume8e30c72007-03-14 11:11:08 +01001574 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001575};
1576
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001577static int __init moschip7720_init(void)
1578{
1579 int retval;
1580
Harvey Harrison441b62c2008-03-03 16:08:34 -08001581 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001582
1583 /* Register with the usb serial */
1584 retval = usb_serial_register(&moschip7720_2port_driver);
1585 if (retval)
1586 goto failed_port_device_register;
1587
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001588 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1589 DRIVER_DESC "\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001590
1591 /* Register with the usb */
1592 retval = usb_register(&usb_driver);
1593 if (retval)
1594 goto failed_usb_register;
1595
1596 return 0;
1597
1598failed_usb_register:
1599 usb_serial_deregister(&moschip7720_2port_driver);
1600
1601failed_port_device_register:
1602 return retval;
1603}
1604
1605static void __exit moschip7720_exit(void)
1606{
1607 usb_deregister(&usb_driver);
1608 usb_serial_deregister(&moschip7720_2port_driver);
1609}
1610
1611module_init(moschip7720_init);
1612module_exit(moschip7720_exit);
1613
1614/* Module information */
Alan Cox4da1a172008-07-22 11:16:21 +01001615MODULE_AUTHOR(DRIVER_AUTHOR);
1616MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001617MODULE_LICENSE("GPL");
1618
1619module_param(debug, bool, S_IRUGO | S_IWUSR);
1620MODULE_PARM_DESC(debug, "Debug enabled or not");