blob: 934458ad55e51782c7b595b48c5f3088393b8495 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky77812a22009-06-16 10:30:29 +02002 * 3215 line mode terminal driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky77812a22009-06-16 10:30:29 +02004 * Copyright IBM Corp. 1999, 2009
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Martin Schwidefsky77812a22009-06-16 10:30:29 +02007 * Updated:
8 * Aug-2000: Added tab support
9 * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kdev_t.h>
15#include <linux/tty.h>
Alan Cox33f0f882006-01-09 20:54:13 -080016#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/vt_kern.h>
18#include <linux/init.h>
19#include <linux/console.h>
20#include <linux/interrupt.h>
Peter Oberparleiter600b5d12006-02-01 03:06:35 -080021#include <linux/err.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020022#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/ccwdev.h>
25#include <asm/cio.h>
26#include <asm/io.h>
27#include <asm/ebcdic.h>
28#include <asm/uaccess.h>
29#include <asm/delay.h>
30#include <asm/cpcmd.h>
31#include <asm/setup.h>
32
33#include "ctrlchar.h"
34
35#define NR_3215 1
36#define NR_3215_REQ (4*NR_3215)
37#define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
38#define RAW3215_INBUF_SIZE 256 /* input buffer size */
39#define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
40#define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
41#define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
42#define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
43#define RAW3215_NR_CCWS 3
44#define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
45
46#define RAW3215_FIXED 1 /* 3215 console device is not be freed */
47#define RAW3215_ACTIVE 2 /* set if the device is in use */
48#define RAW3215_WORKING 4 /* set if a request is being worked on */
49#define RAW3215_THROTTLED 8 /* set if reading is disabled */
50#define RAW3215_STOPPED 16 /* set if writing is disabled */
51#define RAW3215_CLOSING 32 /* set while in close process */
52#define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
53#define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
Martin Schwidefsky77812a22009-06-16 10:30:29 +020054#define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define TAB_STOP_SIZE 8 /* tab stop size */
57
58/*
59 * Request types for a 3215 device
60 */
61enum raw3215_type {
62 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
63};
64
65/*
66 * Request structure for a 3215 device
67 */
68struct raw3215_req {
69 enum raw3215_type type; /* type of the request */
70 int start, len; /* start index & len in output buffer */
71 int delayable; /* indication to wait for more data */
72 int residual; /* residual count for read request */
73 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
74 struct raw3215_info *info; /* pointer to main structure */
75 struct raw3215_req *next; /* pointer to next request */
76} __attribute__ ((aligned(8)));
77
78struct raw3215_info {
79 struct ccw_device *cdev; /* device for tty driver */
80 spinlock_t *lock; /* pointer to irq lock */
81 int flags; /* state flags */
82 char *buffer; /* pointer to output buffer */
83 char *inbuf; /* pointer to input buffer */
84 int head; /* first free byte in output buffer */
85 int count; /* number of bytes in output buffer */
86 int written; /* number of bytes in write requests */
87 struct tty_struct *tty; /* pointer to tty structure if present */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct raw3215_req *queued_read; /* pointer to queued read requests */
89 struct raw3215_req *queued_write;/* pointer to queued write requests */
90 wait_queue_head_t empty_wait; /* wait queue for flushing */
91 struct timer_list timer; /* timer for delayed output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int line_pos; /* position on the line (for tabs) */
93 char ubuffer[80]; /* copy_from_user buffer */
94};
95
96/* array of 3215 devices structures */
97static struct raw3215_info *raw3215[NR_3215];
98/* spinlock to protect the raw3215 array */
99static DEFINE_SPINLOCK(raw3215_device_lock);
100/* list of free request structures */
101static struct raw3215_req *raw3215_freelist;
102/* spinlock to protect free list */
103static spinlock_t raw3215_freelist_lock;
104
105static struct tty_driver *tty3215_driver;
106
107/*
108 * Get a request structure from the free list
109 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200110static inline struct raw3215_req *raw3215_alloc_req(void)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct raw3215_req *req;
113 unsigned long flags;
114
115 spin_lock_irqsave(&raw3215_freelist_lock, flags);
116 req = raw3215_freelist;
117 raw3215_freelist = req->next;
118 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
119 return req;
120}
121
122/*
123 * Put a request structure back to the free list
124 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200125static inline void raw3215_free_req(struct raw3215_req *req)
126{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned long flags;
128
129 if (req->type == RAW3215_FREE)
130 return; /* don't free a free request */
131 req->type = RAW3215_FREE;
132 spin_lock_irqsave(&raw3215_freelist_lock, flags);
133 req->next = raw3215_freelist;
134 raw3215_freelist = req;
135 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
136}
137
138/*
139 * Set up a read request that reads up to 160 byte from the 3215 device.
140 * If there is a queued read request it is used, but that shouldn't happen
141 * because a 3215 terminal won't accept a new read before the old one is
142 * completed.
143 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200144static void raw3215_mk_read_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct raw3215_req *req;
147 struct ccw1 *ccw;
148
149 /* there can only be ONE read request at a time */
150 req = raw->queued_read;
151 if (req == NULL) {
152 /* no queued read request, use new req structure */
153 req = raw3215_alloc_req();
154 req->type = RAW3215_READ;
155 req->info = raw;
156 raw->queued_read = req;
157 }
158
159 ccw = req->ccws;
160 ccw->cmd_code = 0x0A; /* read inquiry */
161 ccw->flags = 0x20; /* ignore incorrect length */
162 ccw->count = 160;
163 ccw->cda = (__u32) __pa(raw->inbuf);
164}
165
166/*
167 * Set up a write request with the information from the main structure.
168 * A ccw chain is created that writes as much as possible from the output
169 * buffer to the 3215 device. If a queued write exists it is replaced by
170 * the new, probably lengthened request.
171 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200172static void raw3215_mk_write_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct raw3215_req *req;
175 struct ccw1 *ccw;
176 int len, count, ix, lines;
177
178 if (raw->count <= raw->written)
179 return;
180 /* check if there is a queued write request */
181 req = raw->queued_write;
182 if (req == NULL) {
183 /* no queued write request, use new req structure */
184 req = raw3215_alloc_req();
185 req->type = RAW3215_WRITE;
186 req->info = raw;
187 raw->queued_write = req;
188 } else {
189 raw->written -= req->len;
190 }
191
192 ccw = req->ccws;
193 req->start = (raw->head - raw->count + raw->written) &
194 (RAW3215_BUFFER_SIZE - 1);
195 /*
196 * now we have to count newlines. We can at max accept
197 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
198 * a restriction in VM
199 */
200 lines = 0;
201 ix = req->start;
202 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
203 if (raw->buffer[ix] == 0x15)
204 lines++;
205 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
206 }
207 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
208 if (len > RAW3215_MAX_BYTES)
209 len = RAW3215_MAX_BYTES;
210 req->len = len;
211 raw->written += len;
212
213 /* set the indication if we should try to enlarge this request */
214 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
215
216 ix = req->start;
217 while (len > 0) {
218 if (ccw > req->ccws)
219 ccw[-1].flags |= 0x40; /* use command chaining */
220 ccw->cmd_code = 0x01; /* write, auto carrier return */
221 ccw->flags = 0x20; /* ignore incorrect length ind. */
222 ccw->cda =
223 (__u32) __pa(raw->buffer + ix);
224 count = len;
225 if (ix + count > RAW3215_BUFFER_SIZE)
226 count = RAW3215_BUFFER_SIZE - ix;
227 ccw->count = count;
228 len -= count;
229 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
230 ccw++;
231 }
232 /*
233 * Add a NOP to the channel program. 3215 devices are purely
234 * emulated and its much better to avoid the channel end
235 * interrupt in this case.
236 */
237 if (ccw > req->ccws)
238 ccw[-1].flags |= 0x40; /* use command chaining */
239 ccw->cmd_code = 0x03; /* NOP */
240 ccw->flags = 0;
241 ccw->cda = 0;
242 ccw->count = 1;
243}
244
245/*
246 * Start a read or a write request
247 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200248static void raw3215_start_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct raw3215_req *req;
251 int res;
252
253 req = raw->queued_read;
254 if (req != NULL &&
255 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
256 /* dequeue request */
257 raw->queued_read = NULL;
258 res = ccw_device_start(raw->cdev, req->ccws,
259 (unsigned long) req, 0, 0);
260 if (res != 0) {
261 /* do_IO failed, put request back to queue */
262 raw->queued_read = req;
263 } else {
264 raw->flags |= RAW3215_WORKING;
265 }
266 }
267 req = raw->queued_write;
268 if (req != NULL &&
269 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
270 /* dequeue request */
271 raw->queued_write = NULL;
272 res = ccw_device_start(raw->cdev, req->ccws,
273 (unsigned long) req, 0, 0);
274 if (res != 0) {
275 /* do_IO failed, put request back to queue */
276 raw->queued_write = req;
277 } else {
278 raw->flags |= RAW3215_WORKING;
279 }
280 }
281}
282
283/*
284 * Function to start a delayed output after RAW3215_TIMEOUT seconds
285 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200286static void raw3215_timeout(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct raw3215_info *raw = (struct raw3215_info *) __data;
289 unsigned long flags;
290
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100291 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (raw->flags & RAW3215_TIMER_RUNS) {
293 del_timer(&raw->timer);
294 raw->flags &= ~RAW3215_TIMER_RUNS;
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200295 if (!(raw->flags & RAW3215_FROZEN)) {
296 raw3215_mk_write_req(raw);
297 raw3215_start_io(raw);
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100300 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/*
304 * Function to conditionally start an IO. A read is started immediately,
305 * a write is only started immediately if the flush flag is on or the
306 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
307 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
308 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200309static inline void raw3215_try_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200311 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return;
313 if (raw->queued_read != NULL)
314 raw3215_start_io(raw);
315 else if (raw->queued_write != NULL) {
316 if ((raw->queued_write->delayable == 0) ||
317 (raw->flags & RAW3215_FLUSHING)) {
318 /* execute write requests bigger than minimum size */
319 raw3215_start_io(raw);
320 if (raw->flags & RAW3215_TIMER_RUNS) {
321 del_timer(&raw->timer);
322 raw->flags &= ~RAW3215_TIMER_RUNS;
323 }
324 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
325 /* delay small writes */
326 init_timer(&raw->timer);
327 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
328 raw->timer.data = (unsigned long) raw;
329 raw->timer.function = raw3215_timeout;
330 add_timer(&raw->timer);
331 raw->flags |= RAW3215_TIMER_RUNS;
332 }
333 }
334}
335
336/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200337 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Heiko Carstens408aec32008-10-10 21:33:28 +0200339static void raw3215_next_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 raw3215_mk_write_req(raw);
344 raw3215_try_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 tty = raw->tty;
346 if (tty != NULL &&
347 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
348 tty_wakeup(tty);
349 }
350}
351
352/*
353 * Interrupt routine, called from common io layer
354 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200355static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
356 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct raw3215_info *raw;
359 struct raw3215_req *req;
360 struct tty_struct *tty;
361 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200362 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700364 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200366 cstat = irb->scsw.cmd.cstat;
367 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200368 if (cstat != 0)
Heiko Carstens408aec32008-10-10 21:33:28 +0200369 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (dstat & 0x01) { /* we got a unit exception */
371 dstat &= ~0x01; /* we can ignore it */
372 }
373 switch (dstat) {
374 case 0x80:
375 if (cstat != 0)
376 break;
377 /* Attention interrupt, someone hit the enter key */
378 raw3215_mk_read_req(raw);
Heiko Carstens408aec32008-10-10 21:33:28 +0200379 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381 case 0x08:
382 case 0x0C:
383 /* Channel end interrupt. */
384 if ((raw = req->info) == NULL)
385 return; /* That shouldn't happen ... */
386 if (req->type == RAW3215_READ) {
387 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200388 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 if (dstat == 0x08)
391 break;
392 case 0x04:
393 /* Device end interrupt. */
394 if ((raw = req->info) == NULL)
395 return; /* That shouldn't happen ... */
396 if (req->type == RAW3215_READ && raw->tty != NULL) {
397 unsigned int cchar;
398
399 tty = raw->tty;
400 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 EBCASC(raw->inbuf, count);
402 cchar = ctrlchar_handle(raw->inbuf, count, tty);
403 switch (cchar & CTRLCHAR_MASK) {
404 case CTRLCHAR_SYSRQ:
405 break;
406
407 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800408 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 tty_flip_buffer_push(raw->tty);
410 break;
411
412 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800414 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
415 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
416 /* add the auto \n */
417 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 count++;
419 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800420 count -= 2;
421 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 tty_flip_buffer_push(raw->tty);
423 break;
424 }
425 } else if (req->type == RAW3215_WRITE) {
426 raw->count -= req->len;
427 raw->written -= req->len;
428 }
429 raw->flags &= ~RAW3215_WORKING;
430 raw3215_free_req(req);
431 /* check for empty wait */
432 if (waitqueue_active(&raw->empty_wait) &&
433 raw->queued_write == NULL &&
434 raw->queued_read == NULL) {
435 wake_up_interruptible(&raw->empty_wait);
436 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200437 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439 default:
440 /* Strange interrupt, I'll do my best to clean up */
441 if (req != NULL && req->type != RAW3215_FREE) {
442 if (req->type == RAW3215_WRITE) {
443 raw->count -= req->len;
444 raw->written -= req->len;
445 }
446 raw->flags &= ~RAW3215_WORKING;
447 raw3215_free_req(req);
448 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200449 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 return;
452}
453
454/*
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200455 * Drop the oldest line from the output buffer.
456 */
457static void raw3215_drop_line(struct raw3215_info *raw)
458{
459 int ix;
460 char ch;
461
462 BUG_ON(raw->written != 0);
463 ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
464 while (raw->count > 0) {
465 ch = raw->buffer[ix];
466 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
467 raw->count--;
468 if (ch == 0x15)
469 break;
470 }
471 raw->head = ix;
472}
473
474/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Wait until length bytes are available int the output buffer.
476 * Has to be called with the s390irq lock held. Can be called
477 * disabled.
478 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200479static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 while (RAW3215_BUFFER_SIZE - raw->count < length) {
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200482 /* While console is frozen for suspend we have no other
483 * choice but to drop message from the buffer to make
484 * room for even more messages. */
485 if (raw->flags & RAW3215_FROZEN) {
486 raw3215_drop_line(raw);
487 continue;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* there might be a request pending */
490 raw->flags |= RAW3215_FLUSHING;
491 raw3215_mk_write_req(raw);
492 raw3215_try_io(raw);
493 raw->flags &= ~RAW3215_FLUSHING;
494#ifdef CONFIG_TN3215_CONSOLE
495 wait_cons_dev();
496#endif
497 /* Enough room freed up ? */
498 if (RAW3215_BUFFER_SIZE - raw->count >= length)
499 break;
500 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100501 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100503 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505}
506
507/*
508 * String write routine for 3215 devices
509 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200510static void raw3215_write(struct raw3215_info *raw, const char *str,
511 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 unsigned long flags;
514 int c, count;
515
516 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100517 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 count = (length > RAW3215_BUFFER_SIZE) ?
519 RAW3215_BUFFER_SIZE : length;
520 length -= count;
521
522 raw3215_make_room(raw, count);
523
524 /* copy string to output buffer and convert it to EBCDIC */
525 while (1) {
526 c = min_t(int, count,
527 min(RAW3215_BUFFER_SIZE - raw->count,
528 RAW3215_BUFFER_SIZE - raw->head));
529 if (c <= 0)
530 break;
531 memcpy(raw->buffer + raw->head, str, c);
532 ASCEBC(raw->buffer + raw->head, c);
533 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
534 raw->count += c;
535 raw->line_pos += c;
536 str += c;
537 count -= c;
538 }
539 if (!(raw->flags & RAW3215_WORKING)) {
540 raw3215_mk_write_req(raw);
541 /* start or queue request */
542 raw3215_try_io(raw);
543 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100544 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546}
547
548/*
549 * Put character routine for 3215 devices
550 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200551static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 unsigned long flags;
554 unsigned int length, i;
555
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100556 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (ch == '\t') {
558 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
559 raw->line_pos += length;
560 ch = ' ';
561 } else if (ch == '\n') {
562 length = 1;
563 raw->line_pos = 0;
564 } else {
565 length = 1;
566 raw->line_pos++;
567 }
568 raw3215_make_room(raw, length);
569
570 for (i = 0; i < length; i++) {
571 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
572 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
573 raw->count++;
574 }
575 if (!(raw->flags & RAW3215_WORKING)) {
576 raw3215_mk_write_req(raw);
577 /* start or queue request */
578 raw3215_try_io(raw);
579 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100580 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583/*
584 * Flush routine, it simply sets the flush flag and tries to start
585 * pending IO.
586 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200587static void raw3215_flush_buffer(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 unsigned long flags;
590
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100591 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (raw->count > 0) {
593 raw->flags |= RAW3215_FLUSHING;
594 raw3215_try_io(raw);
595 raw->flags &= ~RAW3215_FLUSHING;
596 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100597 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
601 * Fire up a 3215 device.
602 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200603static int raw3215_startup(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 unsigned long flags;
606
607 if (raw->flags & RAW3215_ACTIVE)
608 return 0;
609 raw->line_pos = 0;
610 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100611 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100613 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 return 0;
616}
617
618/*
619 * Shutdown a 3215 device.
620 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200621static void raw3215_shutdown(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 DECLARE_WAITQUEUE(wait, current);
624 unsigned long flags;
625
626 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
627 return;
628 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100629 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if ((raw->flags & RAW3215_WORKING) ||
631 raw->queued_write != NULL ||
632 raw->queued_read != NULL) {
633 raw->flags |= RAW3215_CLOSING;
634 add_wait_queue(&raw->empty_wait, &wait);
635 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100636 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100638 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 remove_wait_queue(&raw->empty_wait, &wait);
640 set_current_state(TASK_RUNNING);
641 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
642 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100643 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200646static int raw3215_probe (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct raw3215_info *raw;
649 int line;
650
Cornelia Hucka2e53802007-10-12 16:11:52 +0200651 /* Console is special. */
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700652 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
Cornelia Hucka2e53802007-10-12 16:11:52 +0200653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 raw = kmalloc(sizeof(struct raw3215_info) +
655 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
656 if (raw == NULL)
657 return -ENOMEM;
658
659 spin_lock(&raw3215_device_lock);
660 for (line = 0; line < NR_3215; line++) {
661 if (!raw3215[line]) {
662 raw3215[line] = raw;
663 break;
664 }
665 }
666 spin_unlock(&raw3215_device_lock);
667 if (line == NR_3215) {
668 kfree(raw);
669 return -ENODEV;
670 }
671
672 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
674 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800675 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 GFP_KERNEL|GFP_DMA);
677 if (raw->buffer == NULL) {
678 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200679 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_unlock(&raw3215_device_lock);
681 kfree(raw);
682 return -ENOMEM;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 init_waitqueue_head(&raw->empty_wait);
685
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700686 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 cdev->handler = raw3215_irq;
688
689 return 0;
690}
691
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200692static void raw3215_remove (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct raw3215_info *raw;
695
696 ccw_device_set_offline(cdev);
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700697 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (raw) {
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700699 dev_set_drvdata(&cdev->dev, NULL);
Jesper Juhl17fd6822005-11-07 01:01:30 -0800700 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 kfree(raw);
702 }
703}
704
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200705static int raw3215_set_online (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct raw3215_info *raw;
708
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700709 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!raw)
711 return -ENODEV;
712
713 return raw3215_startup(raw);
714}
715
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200716static int raw3215_set_offline (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct raw3215_info *raw;
719
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700720 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!raw)
722 return -ENODEV;
723
724 raw3215_shutdown(raw);
725
726 return 0;
727}
728
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200729static int raw3215_pm_stop(struct ccw_device *cdev)
730{
731 struct raw3215_info *raw;
732 unsigned long flags;
733
734 /* Empty the output buffer, then prevent new I/O. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200735 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200736 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
737 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
738 raw->flags |= RAW3215_FROZEN;
739 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
740 return 0;
741}
742
743static int raw3215_pm_start(struct ccw_device *cdev)
744{
745 struct raw3215_info *raw;
746 unsigned long flags;
747
748 /* Allow I/O again and flush output buffer. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200749 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200750 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
751 raw->flags &= ~RAW3215_FROZEN;
752 raw->flags |= RAW3215_FLUSHING;
753 raw3215_try_io(raw);
754 raw->flags &= ~RAW3215_FLUSHING;
755 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
756 return 0;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static struct ccw_device_id raw3215_id[] = {
760 { CCW_DEVICE(0x3215, 0) },
761 { /* end of list */ },
762};
763
764static struct ccw_driver raw3215_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +0100765 .driver = {
766 .name = "3215",
767 .owner = THIS_MODULE,
768 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 .ids = raw3215_id,
770 .probe = &raw3215_probe,
771 .remove = &raw3215_remove,
772 .set_online = &raw3215_set_online,
773 .set_offline = &raw3215_set_offline,
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200774 .freeze = &raw3215_pm_stop,
775 .thaw = &raw3215_pm_start,
776 .restore = &raw3215_pm_start,
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100777 .int_class = IOINT_C15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780#ifdef CONFIG_TN3215_CONSOLE
781/*
782 * Write a string to the 3215 console
783 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200784static void con3215_write(struct console *co, const char *str,
785 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct raw3215_info *raw;
788 int i;
789
790 if (count <= 0)
791 return;
792 raw = raw3215[0]; /* console 3215 is the first one */
793 while (count > 0) {
794 for (i = 0; i < count; i++)
795 if (str[i] == '\t' || str[i] == '\n')
796 break;
797 raw3215_write(raw, str, i);
798 count -= i;
799 str += i;
800 if (count > 0) {
801 raw3215_putchar(raw, *str);
802 count--;
803 str++;
804 }
805 }
806}
807
808static struct tty_driver *con3215_device(struct console *c, int *index)
809{
810 *index = c->index;
811 return tty3215_driver;
812}
813
814/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200815 * panic() calls con3215_flush through a panic_notifier
816 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200818static void con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct raw3215_info *raw;
821 unsigned long flags;
822
823 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200824 if (raw->flags & RAW3215_FROZEN)
825 /* The console is still frozen for suspend. */
826 if (ccw_device_force_console())
827 /* Forcing didn't work, no panic message .. */
828 return;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100829 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100831 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Holger Smolinski2332ce12008-10-10 21:33:27 +0200834static int con3215_notify(struct notifier_block *self,
835 unsigned long event, void *data)
836{
837 con3215_flush();
838 return NOTIFY_OK;
839}
840
841static struct notifier_block on_panic_nb = {
842 .notifier_call = con3215_notify,
843 .priority = 0,
844};
845
846static struct notifier_block on_reboot_nb = {
847 .notifier_call = con3215_notify,
848 .priority = 0,
849};
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
852 * The console structure for the 3215 console
853 */
854static struct console con3215 = {
855 .name = "ttyS",
856 .write = con3215_write,
857 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .flags = CON_PRINTBUFFER,
859};
860
861/*
862 * 3215 console initialization code called from console_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200864static int __init con3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct ccw_device *cdev;
867 struct raw3215_info *raw;
868 struct raw3215_req *req;
869 int i;
870
871 /* Check if 3215 is to be the console */
872 if (!CONSOLE_IS_3215)
873 return -ENODEV;
874
875 /* Set the console mode for VM */
876 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700877 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
878 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 /* allocate 3215 request structures */
882 raw3215_freelist = NULL;
883 spin_lock_init(&raw3215_freelist_lock);
884 for (i = 0; i < NR_3215_REQ; i++) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200885 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 req->next = raw3215_freelist;
887 raw3215_freelist = req;
888 }
889
890 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800891 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENODEV;
893
894 raw3215[0] = raw = (struct raw3215_info *)
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200895 kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
896 raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
897 raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 raw->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700899 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 cdev->handler = raw3215_irq;
901
902 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 init_waitqueue_head(&raw->empty_wait);
904
905 /* Request the console irq */
906 if (raw3215_startup(raw) != 0) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200907 kfree(raw->inbuf);
908 kfree(raw->buffer);
909 kfree(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENODEV;
912 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200913 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
914 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 register_console(&con3215);
916 return 0;
917}
918console_initcall(con3215_init);
919#endif
920
921/*
922 * tty3215_open
923 *
924 * This routine is called whenever a 3215 tty is opened.
925 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200926static int tty3215_open(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct raw3215_info *raw;
929 int retval, line;
930
931 line = tty->index;
932 if ((line < 0) || (line >= NR_3215))
933 return -ENODEV;
934
935 raw = raw3215[line];
936 if (raw == NULL)
937 return -ENODEV;
938
939 tty->driver_data = raw;
940 raw->tty = tty;
941
942 tty->low_latency = 0; /* don't use bottom half for pushing chars */
943 /*
944 * Start up 3215 device
945 */
946 retval = raw3215_startup(raw);
947 if (retval)
948 return retval;
949
950 return 0;
951}
952
953/*
954 * tty3215_close()
955 *
956 * This routine is called when the 3215 tty is closed. We wait
957 * for the remaining request to be completed. Then we clean up.
958 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200959static void tty3215_close(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct raw3215_info *raw;
962
963 raw = (struct raw3215_info *) tty->driver_data;
964 if (raw == NULL || tty->count > 1)
965 return;
966 tty->closing = 1;
967 /* Shutdown the terminal */
968 raw3215_shutdown(raw);
969 tty->closing = 0;
970 raw->tty = NULL;
971}
972
973/*
974 * Returns the amount of free space in the output buffer.
975 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200976static int tty3215_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct raw3215_info *raw;
979
980 raw = (struct raw3215_info *) tty->driver_data;
981
982 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
983 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
984 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
985 else
986 return 0;
987}
988
989/*
990 * String write routine for 3215 ttys
991 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200992static int tty3215_write(struct tty_struct * tty,
993 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct raw3215_info *raw;
996
997 if (!tty)
998 return 0;
999 raw = (struct raw3215_info *) tty->driver_data;
1000 raw3215_write(raw, buf, count);
1001 return count;
1002}
1003
1004/*
1005 * Put character routine for 3215 ttys
1006 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001007static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct raw3215_info *raw;
1010
1011 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -07001012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 raw = (struct raw3215_info *) tty->driver_data;
1014 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -07001015 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001018static void tty3215_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020}
1021
1022/*
1023 * Returns the number of characters in the output buffer
1024 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001025static int tty3215_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct raw3215_info *raw;
1028
1029 raw = (struct raw3215_info *) tty->driver_data;
1030 return raw->count;
1031}
1032
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001033static void tty3215_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 struct raw3215_info *raw;
1036
1037 raw = (struct raw3215_info *) tty->driver_data;
1038 raw3215_flush_buffer(raw);
1039 tty_wakeup(tty);
1040}
1041
1042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * Disable reading from a 3215 tty
1044 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001045static void tty3215_throttle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct raw3215_info *raw;
1048
1049 raw = (struct raw3215_info *) tty->driver_data;
1050 raw->flags |= RAW3215_THROTTLED;
1051}
1052
1053/*
1054 * Enable reading from a 3215 tty
1055 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001056static void tty3215_unthrottle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct raw3215_info *raw;
1059 unsigned long flags;
1060
1061 raw = (struct raw3215_info *) tty->driver_data;
1062 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001063 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 raw->flags &= ~RAW3215_THROTTLED;
1065 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001066 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068}
1069
1070/*
1071 * Disable writing to a 3215 tty
1072 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001073static void tty3215_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct raw3215_info *raw;
1076
1077 raw = (struct raw3215_info *) tty->driver_data;
1078 raw->flags |= RAW3215_STOPPED;
1079}
1080
1081/*
1082 * Enable writing to a 3215 tty
1083 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001084static void tty3215_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct raw3215_info *raw;
1087 unsigned long flags;
1088
1089 raw = (struct raw3215_info *) tty->driver_data;
1090 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001091 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 raw->flags &= ~RAW3215_STOPPED;
1093 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001094 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096}
1097
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001098static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 .open = tty3215_open,
1100 .close = tty3215_close,
1101 .write = tty3215_write,
1102 .put_char = tty3215_put_char,
1103 .flush_chars = tty3215_flush_chars,
1104 .write_room = tty3215_write_room,
1105 .chars_in_buffer = tty3215_chars_in_buffer,
1106 .flush_buffer = tty3215_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 .throttle = tty3215_throttle,
1108 .unthrottle = tty3215_unthrottle,
1109 .stop = tty3215_stop,
1110 .start = tty3215_start,
1111};
1112
1113/*
1114 * 3215 tty registration code called from tty_init().
1115 * Most kernel services (incl. kmalloc) are available at this poimt.
1116 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001117static int __init tty3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct tty_driver *driver;
1120 int ret;
1121
1122 if (!CONSOLE_IS_3215)
1123 return 0;
1124
1125 driver = alloc_tty_driver(NR_3215);
1126 if (!driver)
1127 return -ENOMEM;
1128
1129 ret = ccw_driver_register(&raw3215_ccw_driver);
1130 if (ret) {
1131 put_tty_driver(driver);
1132 return ret;
1133 }
1134 /*
1135 * Initialize the tty_driver structure
1136 * Entries in tty3215_driver that are NOT initialized:
1137 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1138 */
1139
1140 driver->owner = THIS_MODULE;
1141 driver->driver_name = "tty3215";
1142 driver->name = "ttyS";
1143 driver->major = TTY_MAJOR;
1144 driver->minor_start = 64;
1145 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1146 driver->subtype = SYSTEM_TYPE_TTY;
1147 driver->init_termios = tty_std_termios;
1148 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1149 driver->init_termios.c_oflag = ONLCR | XTABS;
1150 driver->init_termios.c_lflag = ISIG;
1151 driver->flags = TTY_DRIVER_REAL_RAW;
1152 tty_set_operations(driver, &tty3215_ops);
1153 ret = tty_register_driver(driver);
1154 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 put_tty_driver(driver);
1156 return ret;
1157 }
1158 tty3215_driver = driver;
1159 return 0;
1160}
1161
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001162static void __exit tty3215_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 tty_unregister_driver(tty3215_driver);
1165 put_tty_driver(tty3215_driver);
1166 ccw_driver_unregister(&raw3215_ccw_driver);
1167}
1168
1169module_init(tty3215_init);
1170module_exit(tty3215_exit);