blob: 694464c65fcdaaddd5c7c2b09514ca822e1606af [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
Heiko Carstens12fae582011-01-05 12:47:31 +010012#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kdev_t.h>
16#include <linux/tty.h>
Alan Cox33f0f882006-01-09 20:54:13 -080017#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/vt_kern.h>
19#include <linux/init.h>
20#include <linux/console.h>
21#include <linux/interrupt.h>
Peter Oberparleiter600b5d12006-02-01 03:06:35 -080022#include <linux/err.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020023#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/ccwdev.h>
26#include <asm/cio.h>
27#include <asm/io.h>
28#include <asm/ebcdic.h>
29#include <asm/uaccess.h>
30#include <asm/delay.h>
31#include <asm/cpcmd.h>
32#include <asm/setup.h>
33
34#include "ctrlchar.h"
35
36#define NR_3215 1
37#define NR_3215_REQ (4*NR_3215)
38#define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
39#define RAW3215_INBUF_SIZE 256 /* input buffer size */
40#define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
41#define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
42#define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
43#define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
44#define RAW3215_NR_CCWS 3
45#define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
46
47#define RAW3215_FIXED 1 /* 3215 console device is not be freed */
48#define RAW3215_ACTIVE 2 /* set if the device is in use */
49#define RAW3215_WORKING 4 /* set if a request is being worked on */
50#define RAW3215_THROTTLED 8 /* set if reading is disabled */
51#define RAW3215_STOPPED 16 /* set if writing is disabled */
52#define RAW3215_CLOSING 32 /* set while in close process */
53#define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
54#define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
Martin Schwidefsky77812a22009-06-16 10:30:29 +020055#define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define TAB_STOP_SIZE 8 /* tab stop size */
58
59/*
60 * Request types for a 3215 device
61 */
62enum raw3215_type {
63 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
64};
65
66/*
67 * Request structure for a 3215 device
68 */
69struct raw3215_req {
70 enum raw3215_type type; /* type of the request */
71 int start, len; /* start index & len in output buffer */
72 int delayable; /* indication to wait for more data */
73 int residual; /* residual count for read request */
74 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
75 struct raw3215_info *info; /* pointer to main structure */
76 struct raw3215_req *next; /* pointer to next request */
77} __attribute__ ((aligned(8)));
78
79struct raw3215_info {
80 struct ccw_device *cdev; /* device for tty driver */
81 spinlock_t *lock; /* pointer to irq lock */
82 int flags; /* state flags */
83 char *buffer; /* pointer to output buffer */
84 char *inbuf; /* pointer to input buffer */
85 int head; /* first free byte in output buffer */
86 int count; /* number of bytes in output buffer */
87 int written; /* number of bytes in write requests */
88 struct tty_struct *tty; /* pointer to tty structure if present */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct raw3215_req *queued_read; /* pointer to queued read requests */
90 struct raw3215_req *queued_write;/* pointer to queued write requests */
91 wait_queue_head_t empty_wait; /* wait queue for flushing */
92 struct timer_list timer; /* timer for delayed output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int line_pos; /* position on the line (for tabs) */
94 char ubuffer[80]; /* copy_from_user buffer */
95};
96
97/* array of 3215 devices structures */
98static struct raw3215_info *raw3215[NR_3215];
99/* spinlock to protect the raw3215 array */
100static DEFINE_SPINLOCK(raw3215_device_lock);
101/* list of free request structures */
102static struct raw3215_req *raw3215_freelist;
103/* spinlock to protect free list */
104static spinlock_t raw3215_freelist_lock;
105
106static struct tty_driver *tty3215_driver;
107
108/*
109 * Get a request structure from the free list
110 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200111static inline struct raw3215_req *raw3215_alloc_req(void)
112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct raw3215_req *req;
114 unsigned long flags;
115
116 spin_lock_irqsave(&raw3215_freelist_lock, flags);
117 req = raw3215_freelist;
118 raw3215_freelist = req->next;
119 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
120 return req;
121}
122
123/*
124 * Put a request structure back to the free list
125 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200126static inline void raw3215_free_req(struct raw3215_req *req)
127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned long flags;
129
130 if (req->type == RAW3215_FREE)
131 return; /* don't free a free request */
132 req->type = RAW3215_FREE;
133 spin_lock_irqsave(&raw3215_freelist_lock, flags);
134 req->next = raw3215_freelist;
135 raw3215_freelist = req;
136 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
137}
138
139/*
140 * Set up a read request that reads up to 160 byte from the 3215 device.
141 * If there is a queued read request it is used, but that shouldn't happen
142 * because a 3215 terminal won't accept a new read before the old one is
143 * completed.
144 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200145static void raw3215_mk_read_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct raw3215_req *req;
148 struct ccw1 *ccw;
149
150 /* there can only be ONE read request at a time */
151 req = raw->queued_read;
152 if (req == NULL) {
153 /* no queued read request, use new req structure */
154 req = raw3215_alloc_req();
155 req->type = RAW3215_READ;
156 req->info = raw;
157 raw->queued_read = req;
158 }
159
160 ccw = req->ccws;
161 ccw->cmd_code = 0x0A; /* read inquiry */
162 ccw->flags = 0x20; /* ignore incorrect length */
163 ccw->count = 160;
164 ccw->cda = (__u32) __pa(raw->inbuf);
165}
166
167/*
168 * Set up a write request with the information from the main structure.
169 * A ccw chain is created that writes as much as possible from the output
170 * buffer to the 3215 device. If a queued write exists it is replaced by
171 * the new, probably lengthened request.
172 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200173static void raw3215_mk_write_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct raw3215_req *req;
176 struct ccw1 *ccw;
177 int len, count, ix, lines;
178
179 if (raw->count <= raw->written)
180 return;
181 /* check if there is a queued write request */
182 req = raw->queued_write;
183 if (req == NULL) {
184 /* no queued write request, use new req structure */
185 req = raw3215_alloc_req();
186 req->type = RAW3215_WRITE;
187 req->info = raw;
188 raw->queued_write = req;
189 } else {
190 raw->written -= req->len;
191 }
192
193 ccw = req->ccws;
194 req->start = (raw->head - raw->count + raw->written) &
195 (RAW3215_BUFFER_SIZE - 1);
196 /*
197 * now we have to count newlines. We can at max accept
198 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
199 * a restriction in VM
200 */
201 lines = 0;
202 ix = req->start;
203 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
204 if (raw->buffer[ix] == 0x15)
205 lines++;
206 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
207 }
208 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
209 if (len > RAW3215_MAX_BYTES)
210 len = RAW3215_MAX_BYTES;
211 req->len = len;
212 raw->written += len;
213
214 /* set the indication if we should try to enlarge this request */
215 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
216
217 ix = req->start;
218 while (len > 0) {
219 if (ccw > req->ccws)
220 ccw[-1].flags |= 0x40; /* use command chaining */
221 ccw->cmd_code = 0x01; /* write, auto carrier return */
222 ccw->flags = 0x20; /* ignore incorrect length ind. */
223 ccw->cda =
224 (__u32) __pa(raw->buffer + ix);
225 count = len;
226 if (ix + count > RAW3215_BUFFER_SIZE)
227 count = RAW3215_BUFFER_SIZE - ix;
228 ccw->count = count;
229 len -= count;
230 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
231 ccw++;
232 }
233 /*
234 * Add a NOP to the channel program. 3215 devices are purely
235 * emulated and its much better to avoid the channel end
236 * interrupt in this case.
237 */
238 if (ccw > req->ccws)
239 ccw[-1].flags |= 0x40; /* use command chaining */
240 ccw->cmd_code = 0x03; /* NOP */
241 ccw->flags = 0;
242 ccw->cda = 0;
243 ccw->count = 1;
244}
245
246/*
247 * Start a read or a write request
248 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200249static void raw3215_start_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct raw3215_req *req;
252 int res;
253
254 req = raw->queued_read;
255 if (req != NULL &&
256 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
257 /* dequeue request */
258 raw->queued_read = NULL;
259 res = ccw_device_start(raw->cdev, req->ccws,
260 (unsigned long) req, 0, 0);
261 if (res != 0) {
262 /* do_IO failed, put request back to queue */
263 raw->queued_read = req;
264 } else {
265 raw->flags |= RAW3215_WORKING;
266 }
267 }
268 req = raw->queued_write;
269 if (req != NULL &&
270 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
271 /* dequeue request */
272 raw->queued_write = NULL;
273 res = ccw_device_start(raw->cdev, req->ccws,
274 (unsigned long) req, 0, 0);
275 if (res != 0) {
276 /* do_IO failed, put request back to queue */
277 raw->queued_write = req;
278 } else {
279 raw->flags |= RAW3215_WORKING;
280 }
281 }
282}
283
284/*
285 * Function to start a delayed output after RAW3215_TIMEOUT seconds
286 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200287static void raw3215_timeout(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct raw3215_info *raw = (struct raw3215_info *) __data;
290 unsigned long flags;
291
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100292 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (raw->flags & RAW3215_TIMER_RUNS) {
294 del_timer(&raw->timer);
295 raw->flags &= ~RAW3215_TIMER_RUNS;
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200296 if (!(raw->flags & RAW3215_FROZEN)) {
297 raw3215_mk_write_req(raw);
298 raw3215_start_io(raw);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100301 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/*
305 * Function to conditionally start an IO. A read is started immediately,
306 * a write is only started immediately if the flush flag is on or the
307 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
308 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
309 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200310static inline void raw3215_try_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200312 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return;
314 if (raw->queued_read != NULL)
315 raw3215_start_io(raw);
316 else if (raw->queued_write != NULL) {
317 if ((raw->queued_write->delayable == 0) ||
318 (raw->flags & RAW3215_FLUSHING)) {
319 /* execute write requests bigger than minimum size */
320 raw3215_start_io(raw);
321 if (raw->flags & RAW3215_TIMER_RUNS) {
322 del_timer(&raw->timer);
323 raw->flags &= ~RAW3215_TIMER_RUNS;
324 }
325 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
326 /* delay small writes */
327 init_timer(&raw->timer);
328 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
329 raw->timer.data = (unsigned long) raw;
330 raw->timer.function = raw3215_timeout;
331 add_timer(&raw->timer);
332 raw->flags |= RAW3215_TIMER_RUNS;
333 }
334 }
335}
336
337/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200338 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Heiko Carstens408aec32008-10-10 21:33:28 +0200340static void raw3215_next_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 raw3215_mk_write_req(raw);
345 raw3215_try_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 tty = raw->tty;
347 if (tty != NULL &&
348 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
349 tty_wakeup(tty);
350 }
351}
352
353/*
354 * Interrupt routine, called from common io layer
355 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200356static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
357 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct raw3215_info *raw;
360 struct raw3215_req *req;
361 struct tty_struct *tty;
362 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200363 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Heiko Carstens12fae582011-01-05 12:47:31 +0100365 kstat_cpu(smp_processor_id()).irqs[IOINT_C15]++;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700366 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200368 cstat = irb->scsw.cmd.cstat;
369 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200370 if (cstat != 0)
Heiko Carstens408aec32008-10-10 21:33:28 +0200371 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (dstat & 0x01) { /* we got a unit exception */
373 dstat &= ~0x01; /* we can ignore it */
374 }
375 switch (dstat) {
376 case 0x80:
377 if (cstat != 0)
378 break;
379 /* Attention interrupt, someone hit the enter key */
380 raw3215_mk_read_req(raw);
Heiko Carstens408aec32008-10-10 21:33:28 +0200381 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 case 0x08:
384 case 0x0C:
385 /* Channel end interrupt. */
386 if ((raw = req->info) == NULL)
387 return; /* That shouldn't happen ... */
388 if (req->type == RAW3215_READ) {
389 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200390 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 if (dstat == 0x08)
393 break;
394 case 0x04:
395 /* Device end interrupt. */
396 if ((raw = req->info) == NULL)
397 return; /* That shouldn't happen ... */
398 if (req->type == RAW3215_READ && raw->tty != NULL) {
399 unsigned int cchar;
400
401 tty = raw->tty;
402 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 EBCASC(raw->inbuf, count);
404 cchar = ctrlchar_handle(raw->inbuf, count, tty);
405 switch (cchar & CTRLCHAR_MASK) {
406 case CTRLCHAR_SYSRQ:
407 break;
408
409 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800410 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 tty_flip_buffer_push(raw->tty);
412 break;
413
414 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800416 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
417 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
418 /* add the auto \n */
419 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 count++;
421 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800422 count -= 2;
423 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 tty_flip_buffer_push(raw->tty);
425 break;
426 }
427 } else if (req->type == RAW3215_WRITE) {
428 raw->count -= req->len;
429 raw->written -= req->len;
430 }
431 raw->flags &= ~RAW3215_WORKING;
432 raw3215_free_req(req);
433 /* check for empty wait */
434 if (waitqueue_active(&raw->empty_wait) &&
435 raw->queued_write == NULL &&
436 raw->queued_read == NULL) {
437 wake_up_interruptible(&raw->empty_wait);
438 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200439 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 default:
442 /* Strange interrupt, I'll do my best to clean up */
443 if (req != NULL && req->type != RAW3215_FREE) {
444 if (req->type == RAW3215_WRITE) {
445 raw->count -= req->len;
446 raw->written -= req->len;
447 }
448 raw->flags &= ~RAW3215_WORKING;
449 raw3215_free_req(req);
450 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200451 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 return;
454}
455
456/*
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200457 * Drop the oldest line from the output buffer.
458 */
459static void raw3215_drop_line(struct raw3215_info *raw)
460{
461 int ix;
462 char ch;
463
464 BUG_ON(raw->written != 0);
465 ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
466 while (raw->count > 0) {
467 ch = raw->buffer[ix];
468 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
469 raw->count--;
470 if (ch == 0x15)
471 break;
472 }
473 raw->head = ix;
474}
475
476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * Wait until length bytes are available int the output buffer.
478 * Has to be called with the s390irq lock held. Can be called
479 * disabled.
480 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200481static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 while (RAW3215_BUFFER_SIZE - raw->count < length) {
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200484 /* While console is frozen for suspend we have no other
485 * choice but to drop message from the buffer to make
486 * room for even more messages. */
487 if (raw->flags & RAW3215_FROZEN) {
488 raw3215_drop_line(raw);
489 continue;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* there might be a request pending */
492 raw->flags |= RAW3215_FLUSHING;
493 raw3215_mk_write_req(raw);
494 raw3215_try_io(raw);
495 raw->flags &= ~RAW3215_FLUSHING;
496#ifdef CONFIG_TN3215_CONSOLE
497 wait_cons_dev();
498#endif
499 /* Enough room freed up ? */
500 if (RAW3215_BUFFER_SIZE - raw->count >= length)
501 break;
502 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100503 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100505 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507}
508
509/*
510 * String write routine for 3215 devices
511 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200512static void raw3215_write(struct raw3215_info *raw, const char *str,
513 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 unsigned long flags;
516 int c, count;
517
518 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100519 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 count = (length > RAW3215_BUFFER_SIZE) ?
521 RAW3215_BUFFER_SIZE : length;
522 length -= count;
523
524 raw3215_make_room(raw, count);
525
526 /* copy string to output buffer and convert it to EBCDIC */
527 while (1) {
528 c = min_t(int, count,
529 min(RAW3215_BUFFER_SIZE - raw->count,
530 RAW3215_BUFFER_SIZE - raw->head));
531 if (c <= 0)
532 break;
533 memcpy(raw->buffer + raw->head, str, c);
534 ASCEBC(raw->buffer + raw->head, c);
535 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
536 raw->count += c;
537 raw->line_pos += c;
538 str += c;
539 count -= c;
540 }
541 if (!(raw->flags & RAW3215_WORKING)) {
542 raw3215_mk_write_req(raw);
543 /* start or queue request */
544 raw3215_try_io(raw);
545 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100546 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548}
549
550/*
551 * Put character routine for 3215 devices
552 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200553static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 unsigned long flags;
556 unsigned int length, i;
557
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100558 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (ch == '\t') {
560 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
561 raw->line_pos += length;
562 ch = ' ';
563 } else if (ch == '\n') {
564 length = 1;
565 raw->line_pos = 0;
566 } else {
567 length = 1;
568 raw->line_pos++;
569 }
570 raw3215_make_room(raw, length);
571
572 for (i = 0; i < length; i++) {
573 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
574 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
575 raw->count++;
576 }
577 if (!(raw->flags & RAW3215_WORKING)) {
578 raw3215_mk_write_req(raw);
579 /* start or queue request */
580 raw3215_try_io(raw);
581 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100582 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/*
586 * Flush routine, it simply sets the flush flag and tries to start
587 * pending IO.
588 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200589static void raw3215_flush_buffer(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 unsigned long flags;
592
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100593 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (raw->count > 0) {
595 raw->flags |= RAW3215_FLUSHING;
596 raw3215_try_io(raw);
597 raw->flags &= ~RAW3215_FLUSHING;
598 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100599 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
603 * Fire up a 3215 device.
604 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200605static int raw3215_startup(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 unsigned long flags;
608
609 if (raw->flags & RAW3215_ACTIVE)
610 return 0;
611 raw->line_pos = 0;
612 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100613 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100615 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return 0;
618}
619
620/*
621 * Shutdown a 3215 device.
622 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200623static void raw3215_shutdown(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 DECLARE_WAITQUEUE(wait, current);
626 unsigned long flags;
627
628 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
629 return;
630 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100631 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if ((raw->flags & RAW3215_WORKING) ||
633 raw->queued_write != NULL ||
634 raw->queued_read != NULL) {
635 raw->flags |= RAW3215_CLOSING;
636 add_wait_queue(&raw->empty_wait, &wait);
637 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100638 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100640 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 remove_wait_queue(&raw->empty_wait, &wait);
642 set_current_state(TASK_RUNNING);
643 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
644 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100645 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200648static int raw3215_probe (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct raw3215_info *raw;
651 int line;
652
Cornelia Hucka2e53802007-10-12 16:11:52 +0200653 /* Console is special. */
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700654 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
Cornelia Hucka2e53802007-10-12 16:11:52 +0200655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 raw = kmalloc(sizeof(struct raw3215_info) +
657 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
658 if (raw == NULL)
659 return -ENOMEM;
660
661 spin_lock(&raw3215_device_lock);
662 for (line = 0; line < NR_3215; line++) {
663 if (!raw3215[line]) {
664 raw3215[line] = raw;
665 break;
666 }
667 }
668 spin_unlock(&raw3215_device_lock);
669 if (line == NR_3215) {
670 kfree(raw);
671 return -ENODEV;
672 }
673
674 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
676 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800677 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 GFP_KERNEL|GFP_DMA);
679 if (raw->buffer == NULL) {
680 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200681 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_unlock(&raw3215_device_lock);
683 kfree(raw);
684 return -ENOMEM;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 init_waitqueue_head(&raw->empty_wait);
687
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700688 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 cdev->handler = raw3215_irq;
690
691 return 0;
692}
693
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200694static void raw3215_remove (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct raw3215_info *raw;
697
698 ccw_device_set_offline(cdev);
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700699 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (raw) {
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700701 dev_set_drvdata(&cdev->dev, NULL);
Jesper Juhl17fd6822005-11-07 01:01:30 -0800702 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 kfree(raw);
704 }
705}
706
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200707static int raw3215_set_online (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct raw3215_info *raw;
710
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700711 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!raw)
713 return -ENODEV;
714
715 return raw3215_startup(raw);
716}
717
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200718static int raw3215_set_offline (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct raw3215_info *raw;
721
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700722 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!raw)
724 return -ENODEV;
725
726 raw3215_shutdown(raw);
727
728 return 0;
729}
730
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200731static int raw3215_pm_stop(struct ccw_device *cdev)
732{
733 struct raw3215_info *raw;
734 unsigned long flags;
735
736 /* Empty the output buffer, then prevent new I/O. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200737 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200738 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
739 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
740 raw->flags |= RAW3215_FROZEN;
741 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
742 return 0;
743}
744
745static int raw3215_pm_start(struct ccw_device *cdev)
746{
747 struct raw3215_info *raw;
748 unsigned long flags;
749
750 /* Allow I/O again and flush output buffer. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200751 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200752 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
753 raw->flags &= ~RAW3215_FROZEN;
754 raw->flags |= RAW3215_FLUSHING;
755 raw3215_try_io(raw);
756 raw->flags &= ~RAW3215_FLUSHING;
757 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
758 return 0;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static struct ccw_device_id raw3215_id[] = {
762 { CCW_DEVICE(0x3215, 0) },
763 { /* end of list */ },
764};
765
766static struct ccw_driver raw3215_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +0100767 .driver = {
768 .name = "3215",
769 .owner = THIS_MODULE,
770 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 .ids = raw3215_id,
772 .probe = &raw3215_probe,
773 .remove = &raw3215_remove,
774 .set_online = &raw3215_set_online,
775 .set_offline = &raw3215_set_offline,
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200776 .freeze = &raw3215_pm_stop,
777 .thaw = &raw3215_pm_start,
778 .restore = &raw3215_pm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779};
780
781#ifdef CONFIG_TN3215_CONSOLE
782/*
783 * Write a string to the 3215 console
784 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200785static void con3215_write(struct console *co, const char *str,
786 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct raw3215_info *raw;
789 int i;
790
791 if (count <= 0)
792 return;
793 raw = raw3215[0]; /* console 3215 is the first one */
794 while (count > 0) {
795 for (i = 0; i < count; i++)
796 if (str[i] == '\t' || str[i] == '\n')
797 break;
798 raw3215_write(raw, str, i);
799 count -= i;
800 str += i;
801 if (count > 0) {
802 raw3215_putchar(raw, *str);
803 count--;
804 str++;
805 }
806 }
807}
808
809static struct tty_driver *con3215_device(struct console *c, int *index)
810{
811 *index = c->index;
812 return tty3215_driver;
813}
814
815/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200816 * panic() calls con3215_flush through a panic_notifier
817 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200819static void con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct raw3215_info *raw;
822 unsigned long flags;
823
824 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200825 if (raw->flags & RAW3215_FROZEN)
826 /* The console is still frozen for suspend. */
827 if (ccw_device_force_console())
828 /* Forcing didn't work, no panic message .. */
829 return;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100830 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100832 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Holger Smolinski2332ce12008-10-10 21:33:27 +0200835static int con3215_notify(struct notifier_block *self,
836 unsigned long event, void *data)
837{
838 con3215_flush();
839 return NOTIFY_OK;
840}
841
842static struct notifier_block on_panic_nb = {
843 .notifier_call = con3215_notify,
844 .priority = 0,
845};
846
847static struct notifier_block on_reboot_nb = {
848 .notifier_call = con3215_notify,
849 .priority = 0,
850};
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*
853 * The console structure for the 3215 console
854 */
855static struct console con3215 = {
856 .name = "ttyS",
857 .write = con3215_write,
858 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 .flags = CON_PRINTBUFFER,
860};
861
862/*
863 * 3215 console initialization code called from console_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200865static int __init con3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct ccw_device *cdev;
868 struct raw3215_info *raw;
869 struct raw3215_req *req;
870 int i;
871
872 /* Check if 3215 is to be the console */
873 if (!CONSOLE_IS_3215)
874 return -ENODEV;
875
876 /* Set the console mode for VM */
877 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700878 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
879 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
882 /* allocate 3215 request structures */
883 raw3215_freelist = NULL;
884 spin_lock_init(&raw3215_freelist_lock);
885 for (i = 0; i < NR_3215_REQ; i++) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200886 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 req->next = raw3215_freelist;
888 raw3215_freelist = req;
889 }
890
891 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800892 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -ENODEV;
894
895 raw3215[0] = raw = (struct raw3215_info *)
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200896 kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
897 raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
898 raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 raw->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700900 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 cdev->handler = raw3215_irq;
902
903 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 init_waitqueue_head(&raw->empty_wait);
905
906 /* Request the console irq */
907 if (raw3215_startup(raw) != 0) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200908 kfree(raw->inbuf);
909 kfree(raw->buffer);
910 kfree(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENODEV;
913 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200914 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
915 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 register_console(&con3215);
917 return 0;
918}
919console_initcall(con3215_init);
920#endif
921
922/*
923 * tty3215_open
924 *
925 * This routine is called whenever a 3215 tty is opened.
926 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200927static int tty3215_open(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct raw3215_info *raw;
930 int retval, line;
931
932 line = tty->index;
933 if ((line < 0) || (line >= NR_3215))
934 return -ENODEV;
935
936 raw = raw3215[line];
937 if (raw == NULL)
938 return -ENODEV;
939
940 tty->driver_data = raw;
941 raw->tty = tty;
942
943 tty->low_latency = 0; /* don't use bottom half for pushing chars */
944 /*
945 * Start up 3215 device
946 */
947 retval = raw3215_startup(raw);
948 if (retval)
949 return retval;
950
951 return 0;
952}
953
954/*
955 * tty3215_close()
956 *
957 * This routine is called when the 3215 tty is closed. We wait
958 * for the remaining request to be completed. Then we clean up.
959 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200960static void tty3215_close(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct raw3215_info *raw;
963
964 raw = (struct raw3215_info *) tty->driver_data;
965 if (raw == NULL || tty->count > 1)
966 return;
967 tty->closing = 1;
968 /* Shutdown the terminal */
969 raw3215_shutdown(raw);
970 tty->closing = 0;
971 raw->tty = NULL;
972}
973
974/*
975 * Returns the amount of free space in the output buffer.
976 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200977static int tty3215_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct raw3215_info *raw;
980
981 raw = (struct raw3215_info *) tty->driver_data;
982
983 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
984 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
985 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
986 else
987 return 0;
988}
989
990/*
991 * String write routine for 3215 ttys
992 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200993static int tty3215_write(struct tty_struct * tty,
994 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct raw3215_info *raw;
997
998 if (!tty)
999 return 0;
1000 raw = (struct raw3215_info *) tty->driver_data;
1001 raw3215_write(raw, buf, count);
1002 return count;
1003}
1004
1005/*
1006 * Put character routine for 3215 ttys
1007 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001008static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct raw3215_info *raw;
1011
1012 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -07001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 raw = (struct raw3215_info *) tty->driver_data;
1015 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -07001016 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001019static void tty3215_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021}
1022
1023/*
1024 * Returns the number of characters in the output buffer
1025 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001026static int tty3215_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct raw3215_info *raw;
1029
1030 raw = (struct raw3215_info *) tty->driver_data;
1031 return raw->count;
1032}
1033
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001034static void tty3215_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct raw3215_info *raw;
1037
1038 raw = (struct raw3215_info *) tty->driver_data;
1039 raw3215_flush_buffer(raw);
1040 tty_wakeup(tty);
1041}
1042
1043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * Disable reading from a 3215 tty
1045 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001046static void tty3215_throttle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct raw3215_info *raw;
1049
1050 raw = (struct raw3215_info *) tty->driver_data;
1051 raw->flags |= RAW3215_THROTTLED;
1052}
1053
1054/*
1055 * Enable reading from a 3215 tty
1056 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001057static void tty3215_unthrottle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct raw3215_info *raw;
1060 unsigned long flags;
1061
1062 raw = (struct raw3215_info *) tty->driver_data;
1063 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001064 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 raw->flags &= ~RAW3215_THROTTLED;
1066 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001067 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
1071/*
1072 * Disable writing to a 3215 tty
1073 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001074static void tty3215_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 struct raw3215_info *raw;
1077
1078 raw = (struct raw3215_info *) tty->driver_data;
1079 raw->flags |= RAW3215_STOPPED;
1080}
1081
1082/*
1083 * Enable writing to a 3215 tty
1084 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001085static void tty3215_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct raw3215_info *raw;
1088 unsigned long flags;
1089
1090 raw = (struct raw3215_info *) tty->driver_data;
1091 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001092 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 raw->flags &= ~RAW3215_STOPPED;
1094 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001095 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097}
1098
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001099static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 .open = tty3215_open,
1101 .close = tty3215_close,
1102 .write = tty3215_write,
1103 .put_char = tty3215_put_char,
1104 .flush_chars = tty3215_flush_chars,
1105 .write_room = tty3215_write_room,
1106 .chars_in_buffer = tty3215_chars_in_buffer,
1107 .flush_buffer = tty3215_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 .throttle = tty3215_throttle,
1109 .unthrottle = tty3215_unthrottle,
1110 .stop = tty3215_stop,
1111 .start = tty3215_start,
1112};
1113
1114/*
1115 * 3215 tty registration code called from tty_init().
1116 * Most kernel services (incl. kmalloc) are available at this poimt.
1117 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001118static int __init tty3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct tty_driver *driver;
1121 int ret;
1122
1123 if (!CONSOLE_IS_3215)
1124 return 0;
1125
1126 driver = alloc_tty_driver(NR_3215);
1127 if (!driver)
1128 return -ENOMEM;
1129
1130 ret = ccw_driver_register(&raw3215_ccw_driver);
1131 if (ret) {
1132 put_tty_driver(driver);
1133 return ret;
1134 }
1135 /*
1136 * Initialize the tty_driver structure
1137 * Entries in tty3215_driver that are NOT initialized:
1138 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1139 */
1140
1141 driver->owner = THIS_MODULE;
1142 driver->driver_name = "tty3215";
1143 driver->name = "ttyS";
1144 driver->major = TTY_MAJOR;
1145 driver->minor_start = 64;
1146 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1147 driver->subtype = SYSTEM_TYPE_TTY;
1148 driver->init_termios = tty_std_termios;
1149 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1150 driver->init_termios.c_oflag = ONLCR | XTABS;
1151 driver->init_termios.c_lflag = ISIG;
1152 driver->flags = TTY_DRIVER_REAL_RAW;
1153 tty_set_operations(driver, &tty3215_ops);
1154 ret = tty_register_driver(driver);
1155 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 put_tty_driver(driver);
1157 return ret;
1158 }
1159 tty3215_driver = driver;
1160 return 0;
1161}
1162
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001163static void __exit tty3215_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 tty_unregister_driver(tty3215_driver);
1166 put_tty_driver(tty3215_driver);
1167 ccw_driver_unregister(&raw3215_ccw_driver);
1168}
1169
1170module_init(tty3215_init);
1171module_exit(tty3215_exit);