]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/serial/mos7720.c
MOS7720 has no tiocmget method
[net-next-2.6.git] / drivers / usb / serial / mos7720.c
CommitLineData
0f64478c
GKH
1/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
50d2dc72
GKH
12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
0f64478c
GKH
15 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
4da1a17d 36#include <linux/uaccess.h>
0f64478c
GKH
37
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 */
4da1a17d 67struct moschip_port {
0f64478c
GKH
68 __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 */
4da1a17d 78struct moschip_serial {
0f64478c
GKH
79 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
0f608f89 88static struct usb_device_id moschip_port_id_table[] = {
4da1a17d 89 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
0f64478c
GKH
90 { } /* 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;
81105984 104 int status = urb->status;
325b70c2 105 __u8 *data;
0f64478c
GKH
106 __u8 sp1;
107 __u8 sp2;
0f64478c 108
4da1a17d 109 dbg("%s", " : Entering\n");
0f64478c 110
81105984 111 switch (status) {
0f64478c
GKH
112 case 0:
113 /* success */
114 break;
115 case -ECONNRESET:
116 case -ENOENT:
117 case -ESHUTDOWN:
118 /* this urb is terminated, clean up */
441b62c1 119 dbg("%s - urb shutting down with status: %d", __func__,
81105984 120 status);
0f64478c
GKH
121 return;
122 default:
441b62c1 123 dbg("%s - nonzero urb status received: %d", __func__,
81105984 124 status);
0f64478c
GKH
125 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 */
325b70c2
ON
136
137 /* the above description is inverted
138 * oneukum 2007-03-14 */
139
140 if (unlikely(length != 4)) {
0f64478c
GKH
141 dbg("Wrong data !!!");
142 return;
143 }
144
325b70c2
ON
145 sp1 = data[3];
146 sp2 = data[2];
0f64478c 147
325b70c2 148 if ((sp1 | sp2) & 0x01) {
0f64478c
GKH
149 /* 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",
441b62c1 184 __func__, result);
0f64478c
GKH
185 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{
81105984 195 int retval;
0f64478c
GKH
196 unsigned char *data ;
197 struct usb_serial_port *port;
198 struct moschip_port *mos7720_port;
199 struct tty_struct *tty;
81105984 200 int status = urb->status;
0f64478c 201
81105984
GKH
202 if (status) {
203 dbg("nonzero read bulk status received: %d", status);
0f64478c
GKH
204 return;
205 }
206
207 mos7720_port = urb->context;
208 if (!mos7720_port) {
4da1a17d 209 dbg("%s", "NULL mos7720_port pointer \n");
0f64478c
GKH
210 return ;
211 }
212
213 port = mos7720_port->port;
214
441b62c1 215 dbg("Entering...%s", __func__);
0f64478c
GKH
216
217 data = urb->transfer_buffer;
218
4a90f09b 219 tty = tty_port_tty_get(&port->port);
0f64478c
GKH
220 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 }
4a90f09b 225 tty_kref_put(tty);
0f64478c
GKH
226
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
81105984
GKH
235 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
236 if (retval)
237 dbg("usb_submit_urb(read bulk) failed, retval = %d",
238 retval);
0f64478c
GKH
239 }
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;
81105984 251 int status = urb->status;
0f64478c 252
81105984
GKH
253 if (status) {
254 dbg("nonzero write bulk status received:%d", status);
0f64478c
GKH
255 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
4a90f09b 266 tty = tty_port_tty_get(&mos7720_port->port->port);
0f64478c 267
b963a844
JS
268 if (tty && mos7720_port->open)
269 tty_wakeup(tty);
4a90f09b 270 tty_kref_put(tty);
0f64478c
GKH
271}
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) {
4da1a17d 287 if (product == MOSCHIP_DEVICE_ID_7715)
0f64478c 288 value = value*0x100+0x100;
4da1a17d 289 else
0f64478c 290 value = value*0x100+0x200;
0f64478c
GKH
291 } else {
292 value = 0x0000;
293 if ((product == MOSCHIP_DEVICE_ID_7715) &&
294 (index != 0x08)) {
295 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
4da1a17d 296 /* index = 0x01 ; */
0f64478c
GKH
297 }
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;
4da1a17d 310 pipe = usb_rcvctrlpipe(serial->dev, 0);
0f64478c
GKH
311 }
312
313 status = usb_control_msg(serial->dev, pipe, request, requesttype,
314 value, index, data, size, MOS_WDR_TIMEOUT);
315
316 if (status < 0)
4da1a17d 317 dbg("Command Write failed Value %x index %x\n", value, index);
0f64478c
GKH
318
319 return status;
320}
321
95da310e 322static int mos7720_open(struct tty_struct *tty,
4da1a17d 323 struct usb_serial_port *port, struct file *filp)
0f64478c
GKH
324{
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;
fe4b65ec 333 int allocated_urbs = 0;
0f64478c
GKH
334 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) {
4da1a17d 354 urb = usb_alloc_urb(0, GFP_KERNEL);
0f64478c
GKH
355 mos7720_port->write_urb_pool[j] = urb;
356
357 if (urb == NULL) {
194343d9 358 dev_err(&port->dev, "No more urbs???\n");
0f64478c
GKH
359 continue;
360 }
361
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 GFP_KERNEL);
364 if (!urb->transfer_buffer) {
194343d9
GKH
365 dev_err(&port->dev,
366 "%s-out of memory for urb buffers.\n",
367 __func__);
fe4b65ec
ON
368 usb_free_urb(mos7720_port->write_urb_pool[j]);
369 mos7720_port->write_urb_pool[j] = NULL;
0f64478c
GKH
370 continue;
371 }
fe4b65ec 372 allocated_urbs++;
0f64478c
GKH
373 }
374
fe4b65ec
ON
375 if (!allocated_urbs)
376 return -ENOMEM;
377
0f64478c
GKH
378 /* 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);
4da1a17d 390 dbg("SS::%p LSR:%x\n", mos7720_port, data);
0f64478c
GKH
391
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;
4da1a17d 407 mos7720_port->shadowLCR = data;
0f64478c
GKH
408 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
409 data = 0x0b;
4da1a17d 410 mos7720_port->shadowMCR = data;
0f64478c
GKH
411 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;
4da1a17d
AC
425 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
426 port_number + 1, &data);
0f64478c
GKH
427*/
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;
4da1a17d 435 mos7720_port->shadowLCR = data;
0f64478c
GKH
436 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;
4da1a17d 442 mos7720_port->shadowLCR = data;
0f64478c
GKH
443 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
0f64478c
GKH
449 /* 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,
4da1a17d
AC
462 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);
0f64478c
GKH
468
469 /* start interrupt read for this mos7720 this interrupt *
4da1a17d 470 * will continue as long as the mos7720 is connected */
0f64478c
GKH
471 dbg("Submit URB over !!!");
472 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
473 if (response)
474 dev_err(&port->dev,
898eb71c 475 "%s - Error %d submitting control urb\n",
441b62c1 476 __func__, response);
0f64478c
GKH
477 }
478
479 /* set up our bulk in urb */
480 usb_fill_bulk_urb(port->read_urb, serial->dev,
481 usb_rcvbulkpipe(serial->dev,
4da1a17d 482 port->bulk_in_endpointAddress),
0f64478c
GKH
483 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)
4da1a17d
AC
488 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
489 __func__, response);
0f64478c
GKH
490
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 */
95da310e 512static int mos7720_chars_in_buffer(struct tty_struct *tty)
0f64478c 513{
95da310e 514 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
515 int i;
516 int chars = 0;
517 struct moschip_port *mos7720_port;
518
441b62c1 519 dbg("%s:entering ...........", __func__);
0f64478c
GKH
520
521 mos7720_port = usb_get_serial_port_data(port);
522 if (mos7720_port == NULL) {
441b62c1 523 dbg("%s:leaving ...........", __func__);
23198fda 524 return 0;
0f64478c
GKH
525 }
526
527 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
528 if (mos7720_port->write_urb_pool[i] &&
529 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
0f64478c
GKH
530 chars += URB_TRANSFER_BUFFER_SIZE;
531 }
441b62c1 532 dbg("%s - returns %d", __func__, chars);
0f64478c
GKH
533 return chars;
534}
535
335f8514 536static void mos7720_close(struct usb_serial_port *port)
0f64478c
GKH
537{
538 struct usb_serial *serial;
539 struct moschip_port *mos7720_port;
540 char data;
541 int j;
542
543 dbg("mos7720_close:entering...");
544
545 serial = port->serial;
546
547 mos7720_port = usb_get_serial_port_data(port);
548 if (mos7720_port == NULL)
549 return;
550
551 for (j = 0; j < NUM_URBS; ++j)
552 usb_kill_urb(mos7720_port->write_urb_pool[j]);
553
554 /* Freeing Write URBs */
555 for (j = 0; j < NUM_URBS; ++j) {
556 if (mos7720_port->write_urb_pool[j]) {
557 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
558 usb_free_urb(mos7720_port->write_urb_pool[j]);
559 }
560 }
561
562 /* While closing port, shutdown all bulk read, write *
a1cd7e99
ON
563 * and interrupt read if they exists, otherwise nop */
564 dbg("Shutdown bulk write");
565 usb_kill_urb(port->write_urb);
566 dbg("Shutdown bulk read");
567 usb_kill_urb(port->read_urb);
568
569 mutex_lock(&serial->disc_mutex);
570 /* these commands must not be issued if the device has
571 * been disconnected */
572 if (!serial->disconnected) {
573 data = 0x00;
4da1a17d
AC
574 send_mos_cmd(serial, MOS_WRITE,
575 port->number - port->serial->minor, 0x04, &data);
a1cd7e99
ON
576
577 data = 0x00;
4da1a17d
AC
578 send_mos_cmd(serial, MOS_WRITE,
579 port->number - port->serial->minor, 0x01, &data);
0f64478c 580 }
a1cd7e99 581 mutex_unlock(&serial->disc_mutex);
0f64478c
GKH
582 mos7720_port->open = 0;
583
441b62c1 584 dbg("Leaving %s", __func__);
0f64478c
GKH
585}
586
95da310e 587static void mos7720_break(struct tty_struct *tty, int break_state)
0f64478c 588{
95da310e 589 struct usb_serial_port *port = tty->driver_data;
4da1a17d 590 unsigned char data;
0f64478c
GKH
591 struct usb_serial *serial;
592 struct moschip_port *mos7720_port;
593
441b62c1 594 dbg("Entering %s", __func__);
0f64478c
GKH
595
596 serial = port->serial;
597
598 mos7720_port = usb_get_serial_port_data(port);
599 if (mos7720_port == NULL)
600 return;
601
602 if (break_state == -1)
603 data = mos7720_port->shadowLCR | UART_LCR_SBC;
604 else
605 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
606
607 mos7720_port->shadowLCR = data;
608 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
609 0x03, &data);
610
611 return;
612}
613
614/*
615 * mos7720_write_room
616 * this function is called by the tty driver when it wants to know how many
617 * bytes of data we can accept for a specific port.
618 * If successful, we return the amount of room that we have for this port
619 * Otherwise we return a negative error number.
620 */
95da310e 621static int mos7720_write_room(struct tty_struct *tty)
0f64478c 622{
95da310e 623 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
624 struct moschip_port *mos7720_port;
625 int room = 0;
626 int i;
627
441b62c1 628 dbg("%s:entering ...........", __func__);
0f64478c
GKH
629
630 mos7720_port = usb_get_serial_port_data(port);
631 if (mos7720_port == NULL) {
441b62c1 632 dbg("%s:leaving ...........", __func__);
0f64478c
GKH
633 return -ENODEV;
634 }
635
a5b6f60c 636 /* FIXME: Locking */
0f64478c 637 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
638 if (mos7720_port->write_urb_pool[i] &&
639 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
0f64478c
GKH
640 room += URB_TRANSFER_BUFFER_SIZE;
641 }
642
441b62c1 643 dbg("%s - returns %d", __func__, room);
0f64478c
GKH
644 return room;
645}
646
95da310e
AC
647static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
648 const unsigned char *data, int count)
0f64478c
GKH
649{
650 int status;
651 int i;
652 int bytes_sent = 0;
653 int transfer_size;
654
655 struct moschip_port *mos7720_port;
656 struct usb_serial *serial;
657 struct urb *urb;
658 const unsigned char *current_position = data;
659
441b62c1 660 dbg("%s:entering ...........", __func__);
0f64478c
GKH
661
662 serial = port->serial;
663
664 mos7720_port = usb_get_serial_port_data(port);
665 if (mos7720_port == NULL) {
666 dbg("mos7720_port is NULL");
667 return -ENODEV;
668 }
669
670 /* try to find a free urb in the list */
671 urb = NULL;
672
673 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
674 if (mos7720_port->write_urb_pool[i] &&
675 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
0f64478c 676 urb = mos7720_port->write_urb_pool[i];
4da1a17d 677 dbg("URB:%d", i);
0f64478c
GKH
678 break;
679 }
680 }
681
682 if (urb == NULL) {
441b62c1 683 dbg("%s - no more free urbs", __func__);
0f64478c
GKH
684 goto exit;
685 }
686
687 if (urb->transfer_buffer == NULL) {
688 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
689 GFP_KERNEL);
690 if (urb->transfer_buffer == NULL) {
194343d9
GKH
691 dev_err(&port->dev, "%s no more kernel memory...\n",
692 __func__);
0f64478c
GKH
693 goto exit;
694 }
695 }
4da1a17d 696 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
0f64478c
GKH
697
698 memcpy(urb->transfer_buffer, current_position, transfer_size);
441b62c1 699 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
0f64478c
GKH
700 urb->transfer_buffer);
701
702 /* fill urb with data and submit */
703 usb_fill_bulk_urb(urb, serial->dev,
704 usb_sndbulkpipe(serial->dev,
4da1a17d 705 port->bulk_out_endpointAddress),
0f64478c
GKH
706 urb->transfer_buffer, transfer_size,
707 mos7720_bulk_out_data_callback, mos7720_port);
708
709 /* send it down the pipe */
4da1a17d 710 status = usb_submit_urb(urb, GFP_ATOMIC);
0f64478c 711 if (status) {
194343d9
GKH
712 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
713 "with status = %d\n", __func__, status);
0f64478c
GKH
714 bytes_sent = status;
715 goto exit;
716 }
717 bytes_sent = transfer_size;
718
719exit:
720 return bytes_sent;
721}
722
95da310e 723static void mos7720_throttle(struct tty_struct *tty)
0f64478c 724{
95da310e 725 struct usb_serial_port *port = tty->driver_data;
0f64478c 726 struct moschip_port *mos7720_port;
0f64478c
GKH
727 int status;
728
441b62c1 729 dbg("%s- port %d\n", __func__, port->number);
0f64478c
GKH
730
731 mos7720_port = usb_get_serial_port_data(port);
732
733 if (mos7720_port == NULL)
734 return;
735
736 if (!mos7720_port->open) {
737 dbg("port not opened");
738 return;
739 }
740
441b62c1 741 dbg("%s: Entering ..........", __func__);
0f64478c 742
0f64478c
GKH
743 /* if we are implementing XON/XOFF, send the stop character */
744 if (I_IXOFF(tty)) {
745 unsigned char stop_char = STOP_CHAR(tty);
95da310e 746 status = mos7720_write(tty, port, &stop_char, 1);
0f64478c
GKH
747 if (status <= 0)
748 return;
749 }
750
751 /* if we are implementing RTS/CTS, toggle that line */
752 if (tty->termios->c_cflag & CRTSCTS) {
753 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
754 status = send_mos_cmd(port->serial, MOS_WRITE,
755 port->number - port->serial->minor,
756 UART_MCR, &mos7720_port->shadowMCR);
757 if (status != 0)
758 return;
759 }
760}
761
95da310e 762static void mos7720_unthrottle(struct tty_struct *tty)
0f64478c 763{
95da310e 764 struct usb_serial_port *port = tty->driver_data;
0f64478c 765 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
95da310e 766 int status;
0f64478c
GKH
767
768 if (mos7720_port == NULL)
769 return;
770
771 if (!mos7720_port->open) {
441b62c1 772 dbg("%s - port not opened", __func__);
0f64478c
GKH
773 return;
774 }
775
441b62c1 776 dbg("%s: Entering ..........", __func__);
0f64478c 777
0f64478c
GKH
778 /* if we are implementing XON/XOFF, send the start character */
779 if (I_IXOFF(tty)) {
780 unsigned char start_char = START_CHAR(tty);
95da310e 781 status = mos7720_write(tty, port, &start_char, 1);
0f64478c
GKH
782 if (status <= 0)
783 return;
784 }
785
786 /* if we are implementing RTS/CTS, toggle that line */
787 if (tty->termios->c_cflag & CRTSCTS) {
788 mos7720_port->shadowMCR |= UART_MCR_RTS;
789 status = send_mos_cmd(port->serial, MOS_WRITE,
790 port->number - port->serial->minor,
791 UART_MCR, &mos7720_port->shadowMCR);
792 if (status != 0)
793 return;
794 }
795}
796
797static int set_higher_rates(struct moschip_port *mos7720_port,
798 unsigned int baud)
799{
800 unsigned char data;
801 struct usb_serial_port *port;
802 struct usb_serial *serial;
803 int port_number;
804
805 if (mos7720_port == NULL)
806 return -EINVAL;
807
808 port = mos7720_port->port;
809 serial = port->serial;
810
4da1a17d
AC
811 /***********************************************
812 * Init Sequence for higher rates
813 ***********************************************/
0f64478c
GKH
814 dbg("Sending Setting Commands ..........");
815 port_number = port->number - port->serial->minor;
816
817 data = 0x000;
818 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
819 data = 0x000;
820 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
821 data = 0x0CF;
822 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
823 data = 0x00b;
4da1a17d 824 mos7720_port->shadowMCR = data;
0f64478c
GKH
825 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
826 data = 0x00b;
827 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
828
829 data = 0x000;
830 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
831 data = 0x000;
832 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
833
834
4da1a17d
AC
835 /***********************************************
836 * Set for higher rates *
837 ***********************************************/
0f64478c
GKH
838
839 data = baud * 0x10;
4da1a17d 840 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
0f64478c
GKH
841
842 data = 0x003;
843 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
844 data = 0x003;
845 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
846
847 data = 0x02b;
4da1a17d 848 mos7720_port->shadowMCR = data;
0f64478c
GKH
849 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
850 data = 0x02b;
851 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
852
4da1a17d
AC
853 /***********************************************
854 * Set DLL/DLM
855 ***********************************************/
0f64478c
GKH
856
857 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
4da1a17d 858 mos7720_port->shadowLCR = data;
0f64478c
GKH
859 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
860
861 data = 0x001; /* DLL */
4da1a17d 862 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
0f64478c 863 data = 0x000; /* DLM */
4da1a17d 864 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
0f64478c
GKH
865
866 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
4da1a17d 867 mos7720_port->shadowLCR = data;
0f64478c
GKH
868 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
869
870 return 0;
871}
872
873/* baud rate information */
4da1a17d 874struct divisor_table_entry {
0f64478c
GKH
875 __u32 baudrate;
876 __u16 divisor;
877};
878
879/* Define table of divisors for moschip 7720 hardware *
880 * These assume a 3.6864MHz crystal, the standard /16, and *
881 * MCR.7 = 0. */
882static struct divisor_table_entry divisor_table[] = {
883 { 50, 2304},
884 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
885 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
886 { 150, 768},
887 { 300, 384},
888 { 600, 192},
889 { 1200, 96},
890 { 1800, 64},
891 { 2400, 48},
892 { 4800, 24},
893 { 7200, 16},
894 { 9600, 12},
895 { 19200, 6},
896 { 38400, 3},
897 { 57600, 2},
898 { 115200, 1},
899};
900
901/*****************************************************************************
902 * calc_baud_rate_divisor
903 * this function calculates the proper baud rate divisor for the specified
904 * baud rate.
905 *****************************************************************************/
906static int calc_baud_rate_divisor(int baudrate, int *divisor)
907{
908 int i;
909 __u16 custom;
910 __u16 round1;
911 __u16 round;
912
913
441b62c1 914 dbg("%s - %d", __func__, baudrate);
0f64478c
GKH
915
916 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
917 if (divisor_table[i].baudrate == baudrate) {
918 *divisor = divisor_table[i].divisor;
919 return 0;
920 }
921 }
922
4da1a17d
AC
923 /* After trying for all the standard baud rates *
924 * Try calculating the divisor for this baud rate */
0f64478c
GKH
925 if (baudrate > 75 && baudrate < 230400) {
926 /* get the divisor */
927 custom = (__u16)(230400L / baudrate);
928
929 /* Check for round off */
930 round1 = (__u16)(2304000L / baudrate);
931 round = (__u16)(round1 - (custom * 10));
932 if (round > 4)
933 custom++;
934 *divisor = custom;
935
4da1a17d 936 dbg("Baud %d = %d", baudrate, custom);
0f64478c
GKH
937 return 0;
938 }
939
940 dbg("Baud calculation Failed...");
941 return -EINVAL;
942}
943
944/*
945 * send_cmd_write_baud_rate
946 * this function sends the proper command to change the baud rate of the
947 * specified port.
948 */
949static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
950 int baudrate)
951{
952 struct usb_serial_port *port;
953 struct usb_serial *serial;
954 int divisor;
955 int status;
956 unsigned char data;
957 unsigned char number;
958
959 if (mos7720_port == NULL)
960 return -1;
961
962 port = mos7720_port->port;
963 serial = port->serial;
964
441b62c1 965 dbg("%s: Entering ..........", __func__);
0f64478c
GKH
966
967 number = port->number - port->serial->minor;
441b62c1 968 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
0f64478c 969
4da1a17d 970 /* Calculate the Divisor */
0f64478c
GKH
971 status = calc_baud_rate_divisor(baudrate, &divisor);
972 if (status) {
194343d9 973 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
0f64478c
GKH
974 return status;
975 }
976
4da1a17d
AC
977 /* Enable access to divisor latch */
978 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
979 mos7720_port->shadowLCR = data;
980 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
0f64478c
GKH
981
982 /* Write the divisor */
983 data = ((unsigned char)(divisor & 0xff));
4da1a17d 984 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
0f64478c
GKH
985
986 data = ((unsigned char)((divisor & 0xff00) >> 8));
4da1a17d 987 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
0f64478c 988
4da1a17d
AC
989 /* Disable access to divisor latch */
990 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
991 mos7720_port->shadowLCR = data;
992 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
0f64478c
GKH
993
994 return status;
995}
996
997/*
998 * change_port_settings
999 * This routine is called to set the UART on the device to match
1000 * the specified new settings.
1001 */
95da310e
AC
1002static void change_port_settings(struct tty_struct *tty,
1003 struct moschip_port *mos7720_port,
606d099c 1004 struct ktermios *old_termios)
0f64478c
GKH
1005{
1006 struct usb_serial_port *port;
1007 struct usb_serial *serial;
0f64478c
GKH
1008 int baud;
1009 unsigned cflag;
1010 unsigned iflag;
1011 __u8 mask = 0xff;
1012 __u8 lData;
1013 __u8 lParity;
1014 __u8 lStop;
1015 int status;
1016 int port_number;
1017 char data;
1018
1019 if (mos7720_port == NULL)
1020 return ;
1021
1022 port = mos7720_port->port;
1023 serial = port->serial;
1024 port_number = port->number - port->serial->minor;
1025
441b62c1 1026 dbg("%s - port %d", __func__, port->number);
0f64478c
GKH
1027
1028 if (!mos7720_port->open) {
441b62c1 1029 dbg("%s - port not opened", __func__);
0f64478c
GKH
1030 return;
1031 }
1032
441b62c1 1033 dbg("%s: Entering ..........", __func__);
0f64478c
GKH
1034
1035 lData = UART_LCR_WLEN8;
1036 lStop = 0x00; /* 1 stop bit */
1037 lParity = 0x00; /* No parity */
1038
1039 cflag = tty->termios->c_cflag;
1040 iflag = tty->termios->c_iflag;
1041
1042 /* Change the number of bits */
1043 switch (cflag & CSIZE) {
1044 case CS5:
1045 lData = UART_LCR_WLEN5;
1046 mask = 0x1f;
1047 break;
1048
1049 case CS6:
1050 lData = UART_LCR_WLEN6;
1051 mask = 0x3f;
1052 break;
1053
1054 case CS7:
1055 lData = UART_LCR_WLEN7;
1056 mask = 0x7f;
1057 break;
1058 default:
1059 case CS8:
1060 lData = UART_LCR_WLEN8;
1061 break;
1062 }
1063
1064 /* Change the Parity bit */
1065 if (cflag & PARENB) {
1066 if (cflag & PARODD) {
1067 lParity = UART_LCR_PARITY;
441b62c1 1068 dbg("%s - parity = odd", __func__);
0f64478c
GKH
1069 } else {
1070 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
441b62c1 1071 dbg("%s - parity = even", __func__);
0f64478c
GKH
1072 }
1073
1074 } else {
441b62c1 1075 dbg("%s - parity = none", __func__);
0f64478c
GKH
1076 }
1077
1078 if (cflag & CMSPAR)
1079 lParity = lParity | 0x20;
1080
1081 /* Change the Stop bit */
1082 if (cflag & CSTOPB) {
1083 lStop = UART_LCR_STOP;
441b62c1 1084 dbg("%s - stop bits = 2", __func__);
0f64478c
GKH
1085 } else {
1086 lStop = 0x00;
441b62c1 1087 dbg("%s - stop bits = 1", __func__);
0f64478c
GKH
1088 }
1089
1090#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1091#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1092#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1093
1094 /* Update the LCR with the correct value */
4da1a17d
AC
1095 mos7720_port->shadowLCR &=
1096 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
0f64478c
GKH
1097 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1098
1099
1100 /* Disable Interrupts */
1101 data = 0x00;
4da1a17d
AC
1102 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1103 UART_IER, &data);
0f64478c
GKH
1104
1105 data = 0x00;
4da1a17d 1106 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
0f64478c
GKH
1107
1108 data = 0xcf;
4da1a17d 1109 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
0f64478c
GKH
1110
1111 /* Send the updated LCR value to the mos7720 */
1112 data = mos7720_port->shadowLCR;
4da1a17d 1113 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
0f64478c 1114
4da1a17d
AC
1115 data = 0x00b;
1116 mos7720_port->shadowMCR = data;
1117 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1118 data = 0x00b;
1119 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
0f64478c
GKH
1120
1121 /* set up the MCR register and send it to the mos7720 */
1122 mos7720_port->shadowMCR = UART_MCR_OUT2;
1123 if (cflag & CBAUD)
1124 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1125
1126 if (cflag & CRTSCTS) {
1127 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
4da1a17d
AC
1128 /* To set hardware flow control to the specified *
1129 * serial port, in SP1/2_CONTROL_REG */
0f64478c
GKH
1130 if (port->number) {
1131 data = 0x001;
1132 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1133 0x08, &data);
1134 } else {
1135 data = 0x002;
1136 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1137 0x08, &data);
1138 }
1139 } else {
1140 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1141 }
1142
1143 data = mos7720_port->shadowMCR;
1144 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1145
1146 /* Determine divisor based on baud rate */
1147 baud = tty_get_baud_rate(tty);
1148 if (!baud) {
1149 /* pick a default, any default... */
1150 dbg("Picked default baud...");
1151 baud = 9600;
1152 }
1153
1154 if (baud >= 230400) {
1155 set_higher_rates(mos7720_port, baud);
1156 /* Enable Interrupts */
1157 data = 0x0c;
1158 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1159 return;
1160 }
1161
441b62c1 1162 dbg("%s - baud rate = %d", __func__, baud);
0f64478c 1163 status = send_cmd_write_baud_rate(mos7720_port, baud);
65d063ab
AC
1164 /* FIXME: needs to write actual resulting baud back not just
1165 blindly do so */
1166 if (cflag & CBAUD)
1167 tty_encode_baud_rate(tty, baud, baud);
0f64478c
GKH
1168 /* Enable Interrupts */
1169 data = 0x0c;
1170 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1171
1172 if (port->read_urb->status != -EINPROGRESS) {
1173 port->read_urb->dev = serial->dev;
1174
1175 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1176 if (status)
1177 dbg("usb_submit_urb(read bulk) failed, status = %d",
1178 status);
1179 }
1180 return;
1181}
1182
1183/*
1184 * mos7720_set_termios
1185 * this function is called by the tty driver when it wants to change the
1186 * termios structure.
1187 */
95da310e
AC
1188static void mos7720_set_termios(struct tty_struct *tty,
1189 struct usb_serial_port *port, struct ktermios *old_termios)
0f64478c
GKH
1190{
1191 int status;
1192 unsigned int cflag;
1193 struct usb_serial *serial;
1194 struct moschip_port *mos7720_port;
0f64478c
GKH
1195
1196 serial = port->serial;
1197
1198 mos7720_port = usb_get_serial_port_data(port);
1199
1200 if (mos7720_port == NULL)
1201 return;
1202
0f64478c 1203 if (!mos7720_port->open) {
441b62c1 1204 dbg("%s - port not opened", __func__);
0f64478c
GKH
1205 return;
1206 }
1207
4da1a17d 1208 dbg("%s\n", "setting termios - ASPIRE");
0f64478c
GKH
1209
1210 cflag = tty->termios->c_cflag;
1211
441b62c1 1212 dbg("%s - cflag %08x iflag %08x", __func__,
0f64478c
GKH
1213 tty->termios->c_cflag,
1214 RELEVANT_IFLAG(tty->termios->c_iflag));
1215
441b62c1 1216 dbg("%s - old cflag %08x old iflag %08x", __func__,
65d063ab
AC
1217 old_termios->c_cflag,
1218 RELEVANT_IFLAG(old_termios->c_iflag));
0f64478c 1219
441b62c1 1220 dbg("%s - port %d", __func__, port->number);
0f64478c
GKH
1221
1222 /* change the port settings to the new ones specified */
95da310e 1223 change_port_settings(tty, mos7720_port, old_termios);
0f64478c 1224
4da1a17d
AC
1225 if (!port->read_urb) {
1226 dbg("%s", "URB KILLED !!!!!\n");
0f64478c
GKH
1227 return;
1228 }
1229
4da1a17d 1230 if (port->read_urb->status != -EINPROGRESS) {
0f64478c
GKH
1231 port->read_urb->dev = serial->dev;
1232 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1233 if (status)
1234 dbg("usb_submit_urb(read bulk) failed, status = %d",
1235 status);
1236 }
1237 return;
1238}
1239
1240/*
1241 * get_lsr_info - get line status register info
1242 *
1243 * Purpose: Let user call ioctl() to get info when the UART physically
1244 * is emptied. On bus types like RS485, the transmitter must
1245 * release the bus after transmitting. This must be done when
1246 * the transmit shift register is empty, not be done when the
1247 * transmit holding register is empty. This functionality
1248 * allows an RS485 driver to be written in user space.
1249 */
4da1a17d
AC
1250static int get_lsr_info(struct tty_struct *tty,
1251 struct moschip_port *mos7720_port, unsigned int __user *value)
0f64478c
GKH
1252{
1253 int count;
1254 unsigned int result = 0;
1255
95da310e 1256 count = mos7720_chars_in_buffer(tty);
0f64478c 1257 if (count == 0) {
441b62c1 1258 dbg("%s -- Empty", __func__);
0f64478c
GKH
1259 result = TIOCSER_TEMT;
1260 }
1261
1262 if (copy_to_user(value, &result, sizeof(int)))
1263 return -EFAULT;
1264 return 0;
1265}
1266
0f608f89
KS
1267static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1268{
1269 struct usb_serial_port *port = tty->driver_data;
1270 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1271 unsigned int result = 0;
1272 unsigned int mcr ;
1273 unsigned int msr ;
1274
1275 dbg("%s - port %d", __func__, port->number);
1276
1277 mcr = mos7720_port->shadowMCR;
1278 msr = mos7720_port->shadowMSR;
1279
1280 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1281 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1282 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1283 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1284 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1285 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1286
1287 dbg("%s -- %x", __func__, result);
1288
1289 return result;
1290}
1291
1292static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1293 unsigned int set, unsigned int clear)
1294{
1295 struct usb_serial_port *port = tty->driver_data;
1296 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1297 unsigned int mcr ;
1298 unsigned char lmcr;
1299
1300 dbg("%s - port %d", __func__, port->number);
1301 dbg("he was at tiocmget");
1302
1303 mcr = mos7720_port->shadowMCR;
1304
1305 if (set & TIOCM_RTS)
1306 mcr |= UART_MCR_RTS;
1307 if (set & TIOCM_DTR)
1308 mcr |= UART_MCR_DTR;
1309 if (set & TIOCM_LOOP)
1310 mcr |= UART_MCR_LOOP;
1311
1312 if (clear & TIOCM_RTS)
1313 mcr &= ~UART_MCR_RTS;
1314 if (clear & TIOCM_DTR)
1315 mcr &= ~UART_MCR_DTR;
1316 if (clear & TIOCM_LOOP)
1317 mcr &= ~UART_MCR_LOOP;
1318
1319 mos7720_port->shadowMCR = mcr;
1320 lmcr = mos7720_port->shadowMCR;
1321
1322 send_mos_cmd(port->serial, MOS_WRITE,
1323 port->number - port->serial->minor, UART_MCR, &lmcr);
1324
1325 return 0;
1326}
1327
0f64478c
GKH
1328static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1329 unsigned int __user *value)
1330{
1331 unsigned int mcr ;
1332 unsigned int arg;
1333 unsigned char data;
1334
1335 struct usb_serial_port *port;
1336
1337 if (mos7720_port == NULL)
1338 return -1;
1339
4da1a17d 1340 port = (struct usb_serial_port *)mos7720_port->port;
0f64478c
GKH
1341 mcr = mos7720_port->shadowMCR;
1342
1343 if (copy_from_user(&arg, value, sizeof(int)))
1344 return -EFAULT;
1345
1346 switch (cmd) {
1347 case TIOCMBIS:
1348 if (arg & TIOCM_RTS)
1349 mcr |= UART_MCR_RTS;
1350 if (arg & TIOCM_DTR)
1351 mcr |= UART_MCR_RTS;
1352 if (arg & TIOCM_LOOP)
1353 mcr |= UART_MCR_LOOP;
1354 break;
1355
1356 case TIOCMBIC:
1357 if (arg & TIOCM_RTS)
1358 mcr &= ~UART_MCR_RTS;
1359 if (arg & TIOCM_DTR)
1360 mcr &= ~UART_MCR_RTS;
1361 if (arg & TIOCM_LOOP)
1362 mcr &= ~UART_MCR_LOOP;
1363 break;
1364
0f64478c
GKH
1365 }
1366
1367 mos7720_port->shadowMCR = mcr;
1368
1369 data = mos7720_port->shadowMCR;
1370 send_mos_cmd(port->serial, MOS_WRITE,
1371 port->number - port->serial->minor, UART_MCR, &data);
1372
1373 return 0;
1374}
1375
0f64478c
GKH
1376static int get_serial_info(struct moschip_port *mos7720_port,
1377 struct serial_struct __user *retinfo)
1378{
1379 struct serial_struct tmp;
1380
1381 if (!retinfo)
1382 return -EFAULT;
1383
1384 memset(&tmp, 0, sizeof(tmp));
1385
1386 tmp.type = PORT_16550A;
1387 tmp.line = mos7720_port->port->serial->minor;
1388 tmp.port = mos7720_port->port->number;
1389 tmp.irq = 0;
1390 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
4da1a17d 1391 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
0f64478c
GKH
1392 tmp.baud_base = 9600;
1393 tmp.close_delay = 5*HZ;
1394 tmp.closing_wait = 30*HZ;
1395
1396 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1397 return -EFAULT;
1398 return 0;
1399}
1400
95da310e 1401static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
0f64478c
GKH
1402 unsigned int cmd, unsigned long arg)
1403{
95da310e 1404 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
1405 struct moschip_port *mos7720_port;
1406 struct async_icount cnow;
1407 struct async_icount cprev;
1408 struct serial_icounter_struct icount;
1409
1410 mos7720_port = usb_get_serial_port_data(port);
1411 if (mos7720_port == NULL)
1412 return -ENODEV;
1413
441b62c1 1414 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
0f64478c
GKH
1415
1416 switch (cmd) {
0f64478c 1417 case TIOCSERGETLSR:
441b62c1 1418 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
4da1a17d
AC
1419 return get_lsr_info(tty, mos7720_port,
1420 (unsigned int __user *)arg);
0f64478c
GKH
1421 return 0;
1422
95da310e 1423 /* FIXME: These should be using the mode methods */
0f64478c
GKH
1424 case TIOCMBIS:
1425 case TIOCMBIC:
4da1a17d
AC
1426 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1427 __func__, port->number);
0f64478c
GKH
1428 return set_modem_info(mos7720_port, cmd,
1429 (unsigned int __user *)arg);
1430
0f64478c 1431 case TIOCGSERIAL:
441b62c1 1432 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
0f64478c
GKH
1433 return get_serial_info(mos7720_port,
1434 (struct serial_struct __user *)arg);
1435
0f64478c 1436 case TIOCMIWAIT:
441b62c1 1437 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
0f64478c
GKH
1438 cprev = mos7720_port->icount;
1439 while (1) {
1440 if (signal_pending(current))
1441 return -ERESTARTSYS;
1442 cnow = mos7720_port->icount;
1443 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1444 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1445 return -EIO; /* no change => error */
1446 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1447 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1448 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
4da1a17d 1449 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
0f64478c
GKH
1450 return 0;
1451 }
1452 cprev = cnow;
1453 }
1454 /* NOTREACHED */
1455 break;
1456
1457 case TIOCGICOUNT:
1458 cnow = mos7720_port->icount;
1459 icount.cts = cnow.cts;
1460 icount.dsr = cnow.dsr;
1461 icount.rng = cnow.rng;
1462 icount.dcd = cnow.dcd;
1463 icount.rx = cnow.rx;
1464 icount.tx = cnow.tx;
1465 icount.frame = cnow.frame;
1466 icount.overrun = cnow.overrun;
1467 icount.parity = cnow.parity;
1468 icount.brk = cnow.brk;
1469 icount.buf_overrun = cnow.buf_overrun;
1470
441b62c1 1471 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
4da1a17d 1472 port->number, icount.rx, icount.tx);
0f64478c
GKH
1473 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1474 return -EFAULT;
1475 return 0;
1476 }
1477
1478 return -ENOIOCTLCMD;
1479}
1480
1481static int mos7720_startup(struct usb_serial *serial)
1482{
1483 struct moschip_serial *mos7720_serial;
1484 struct moschip_port *mos7720_port;
1485 struct usb_device *dev;
1486 int i;
1487 char data;
1488
441b62c1 1489 dbg("%s: Entering ..........", __func__);
0f64478c
GKH
1490
1491 if (!serial) {
1492 dbg("Invalid Handler");
1493 return -ENODEV;
1494 }
1495
1496 dev = serial->dev;
1497
1498 /* create our private serial structure */
1499 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1500 if (mos7720_serial == NULL) {
194343d9 1501 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
0f64478c
GKH
1502 return -ENOMEM;
1503 }
1504
1505 usb_set_serial_data(serial, mos7720_serial);
1506
1507 /* we set up the pointers to the endpoints in the mos7720_open *
1508 * function, as the structures aren't created yet. */
1509
1510 /* set up port private structures */
1511 for (i = 0; i < serial->num_ports; ++i) {
1512 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1513 if (mos7720_port == NULL) {
194343d9 1514 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
0f64478c
GKH
1515 usb_set_serial_data(serial, NULL);
1516 kfree(mos7720_serial);
1517 return -ENOMEM;
1518 }
1519
1520 /* Initialize all port interrupt end point to port 0 int
1521 * endpoint. Our device has only one interrupt endpoint
1522 * comman to all ports */
4da1a17d
AC
1523 serial->port[i]->interrupt_in_endpointAddress =
1524 serial->port[0]->interrupt_in_endpointAddress;
0f64478c
GKH
1525
1526 mos7720_port->port = serial->port[i];
1527 usb_set_serial_port_data(serial->port[i], mos7720_port);
1528
1529 dbg("port number is %d", serial->port[i]->number);
1530 dbg("serial number is %d", serial->minor);
1531 }
1532
1533
1534 /* setting configuration feature to one */
1535 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
4da1a17d 1536 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
0f64478c 1537
4da1a17d
AC
1538 /* LSR For Port 1 */
1539 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1540 dbg("LSR:%x", data);
0f64478c 1541
4da1a17d
AC
1542 /* LSR For Port 2 */
1543 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1544 dbg("LSR:%x", data);
0f64478c
GKH
1545
1546 return 0;
1547}
1548
f9c99bb8 1549static void mos7720_release(struct usb_serial *serial)
0f64478c
GKH
1550{
1551 int i;
1552
1553 /* free private structure allocated for serial port */
f9c99bb8 1554 for (i = 0; i < serial->num_ports; ++i)
0f64478c 1555 kfree(usb_get_serial_port_data(serial->port[i]));
0f64478c
GKH
1556
1557 /* free private structure allocated for serial device */
1558 kfree(usb_get_serial_data(serial));
0f64478c
GKH
1559}
1560
d9b1b787
JH
1561static struct usb_driver usb_driver = {
1562 .name = "moschip7720",
1563 .probe = usb_serial_probe,
1564 .disconnect = usb_serial_disconnect,
1565 .id_table = moschip_port_id_table,
1566 .no_dynamic_id = 1,
1567};
1568
0f64478c
GKH
1569static struct usb_serial_driver moschip7720_2port_driver = {
1570 .driver = {
1571 .owner = THIS_MODULE,
1572 .name = "moschip7720",
1573 },
1574 .description = "Moschip 2 port adapter",
d9b1b787 1575 .usb_driver = &usb_driver,
0f64478c 1576 .id_table = moschip_port_id_table,
0f64478c
GKH
1577 .num_ports = 2,
1578 .open = mos7720_open,
1579 .close = mos7720_close,
1580 .throttle = mos7720_throttle,
1581 .unthrottle = mos7720_unthrottle,
1582 .attach = mos7720_startup,
f9c99bb8 1583 .release = mos7720_release,
0f64478c 1584 .ioctl = mos7720_ioctl,
0f608f89
KS
1585 .tiocmget = mos7720_tiocmget,
1586 .tiocmset = mos7720_tiocmset,
0f64478c
GKH
1587 .set_termios = mos7720_set_termios,
1588 .write = mos7720_write,
1589 .write_room = mos7720_write_room,
1590 .chars_in_buffer = mos7720_chars_in_buffer,
1591 .break_ctl = mos7720_break,
1592 .read_bulk_callback = mos7720_bulk_in_callback,
e8e30c76 1593 .read_int_callback = mos7720_interrupt_callback,
0f64478c
GKH
1594};
1595
0f64478c
GKH
1596static int __init moschip7720_init(void)
1597{
1598 int retval;
1599
441b62c1 1600 dbg("%s: Entering ..........", __func__);
0f64478c
GKH
1601
1602 /* Register with the usb serial */
1603 retval = usb_serial_register(&moschip7720_2port_driver);
1604 if (retval)
1605 goto failed_port_device_register;
1606
c197a8db
GKH
1607 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1608 DRIVER_DESC "\n");
0f64478c
GKH
1609
1610 /* Register with the usb */
1611 retval = usb_register(&usb_driver);
1612 if (retval)
1613 goto failed_usb_register;
1614
1615 return 0;
1616
1617failed_usb_register:
1618 usb_serial_deregister(&moschip7720_2port_driver);
1619
1620failed_port_device_register:
1621 return retval;
1622}
1623
1624static void __exit moschip7720_exit(void)
1625{
1626 usb_deregister(&usb_driver);
1627 usb_serial_deregister(&moschip7720_2port_driver);
1628}
1629
1630module_init(moschip7720_init);
1631module_exit(moschip7720_exit);
1632
1633/* Module information */
4da1a17d
AC
1634MODULE_AUTHOR(DRIVER_AUTHOR);
1635MODULE_DESCRIPTION(DRIVER_DESC);
0f64478c
GKH
1636MODULE_LICENSE("GPL");
1637
1638module_param(debug, bool, S_IRUGO | S_IWUSR);
1639MODULE_PARM_DESC(debug, "Debug enabled or not");