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