blob: e71a50d4b2217b22fb262338f31aa1fe98931349 [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 */
Martin Schwidefsky656d9122012-02-17 10:29:22 +010090 struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 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/*
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100338 * Call tty_wakeup from tasklet context
339 */
340static void raw3215_wakeup(unsigned long data)
341{
342 struct raw3215_info *raw = (struct raw3215_info *) data;
343 tty_wakeup(raw->tty);
344}
345
346/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200347 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Heiko Carstens408aec32008-10-10 21:33:28 +0200349static void raw3215_next_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 raw3215_mk_write_req(raw);
352 raw3215_try_io(raw);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100353 if (raw->tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
354 tasklet_schedule(&raw->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Interrupt routine, called from common io layer
359 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200360static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
361 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct raw3215_info *raw;
364 struct raw3215_req *req;
365 struct tty_struct *tty;
366 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200367 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700369 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200371 cstat = irb->scsw.cmd.cstat;
372 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200373 if (cstat != 0)
Heiko Carstens408aec32008-10-10 21:33:28 +0200374 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (dstat & 0x01) { /* we got a unit exception */
376 dstat &= ~0x01; /* we can ignore it */
377 }
378 switch (dstat) {
379 case 0x80:
380 if (cstat != 0)
381 break;
382 /* Attention interrupt, someone hit the enter key */
383 raw3215_mk_read_req(raw);
Heiko Carstens408aec32008-10-10 21:33:28 +0200384 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 case 0x08:
387 case 0x0C:
388 /* Channel end interrupt. */
389 if ((raw = req->info) == NULL)
390 return; /* That shouldn't happen ... */
391 if (req->type == RAW3215_READ) {
392 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200393 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 if (dstat == 0x08)
396 break;
397 case 0x04:
398 /* Device end interrupt. */
399 if ((raw = req->info) == NULL)
400 return; /* That shouldn't happen ... */
401 if (req->type == RAW3215_READ && raw->tty != NULL) {
402 unsigned int cchar;
403
404 tty = raw->tty;
405 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 EBCASC(raw->inbuf, count);
407 cchar = ctrlchar_handle(raw->inbuf, count, tty);
408 switch (cchar & CTRLCHAR_MASK) {
409 case CTRLCHAR_SYSRQ:
410 break;
411
412 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800413 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 tty_flip_buffer_push(raw->tty);
415 break;
416
417 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800419 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
420 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
421 /* add the auto \n */
422 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 count++;
424 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800425 count -= 2;
426 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 tty_flip_buffer_push(raw->tty);
428 break;
429 }
430 } else if (req->type == RAW3215_WRITE) {
431 raw->count -= req->len;
432 raw->written -= req->len;
433 }
434 raw->flags &= ~RAW3215_WORKING;
435 raw3215_free_req(req);
436 /* check for empty wait */
437 if (waitqueue_active(&raw->empty_wait) &&
438 raw->queued_write == NULL &&
439 raw->queued_read == NULL) {
440 wake_up_interruptible(&raw->empty_wait);
441 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200442 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 default:
445 /* Strange interrupt, I'll do my best to clean up */
446 if (req != NULL && req->type != RAW3215_FREE) {
447 if (req->type == RAW3215_WRITE) {
448 raw->count -= req->len;
449 raw->written -= req->len;
450 }
451 raw->flags &= ~RAW3215_WORKING;
452 raw3215_free_req(req);
453 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200454 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 return;
457}
458
459/*
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200460 * Drop the oldest line from the output buffer.
461 */
462static void raw3215_drop_line(struct raw3215_info *raw)
463{
464 int ix;
465 char ch;
466
467 BUG_ON(raw->written != 0);
468 ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
469 while (raw->count > 0) {
470 ch = raw->buffer[ix];
471 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
472 raw->count--;
473 if (ch == 0x15)
474 break;
475 }
476 raw->head = ix;
477}
478
479/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Wait until length bytes are available int the output buffer.
481 * Has to be called with the s390irq lock held. Can be called
482 * disabled.
483 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200484static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 while (RAW3215_BUFFER_SIZE - raw->count < length) {
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200487 /* While console is frozen for suspend we have no other
488 * choice but to drop message from the buffer to make
489 * room for even more messages. */
490 if (raw->flags & RAW3215_FROZEN) {
491 raw3215_drop_line(raw);
492 continue;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* there might be a request pending */
495 raw->flags |= RAW3215_FLUSHING;
496 raw3215_mk_write_req(raw);
497 raw3215_try_io(raw);
498 raw->flags &= ~RAW3215_FLUSHING;
499#ifdef CONFIG_TN3215_CONSOLE
500 wait_cons_dev();
501#endif
502 /* Enough room freed up ? */
503 if (RAW3215_BUFFER_SIZE - raw->count >= length)
504 break;
505 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100506 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100508 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510}
511
512/*
513 * String write routine for 3215 devices
514 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200515static void raw3215_write(struct raw3215_info *raw, const char *str,
516 unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned long flags;
519 int c, count;
520
521 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100522 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 count = (length > RAW3215_BUFFER_SIZE) ?
524 RAW3215_BUFFER_SIZE : length;
525 length -= count;
526
527 raw3215_make_room(raw, count);
528
529 /* copy string to output buffer and convert it to EBCDIC */
530 while (1) {
531 c = min_t(int, count,
532 min(RAW3215_BUFFER_SIZE - raw->count,
533 RAW3215_BUFFER_SIZE - raw->head));
534 if (c <= 0)
535 break;
536 memcpy(raw->buffer + raw->head, str, c);
537 ASCEBC(raw->buffer + raw->head, c);
538 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
539 raw->count += c;
540 raw->line_pos += c;
541 str += c;
542 count -= c;
543 }
544 if (!(raw->flags & RAW3215_WORKING)) {
545 raw3215_mk_write_req(raw);
546 /* start or queue request */
547 raw3215_try_io(raw);
548 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100549 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551}
552
553/*
554 * Put character routine for 3215 devices
555 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200556static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned long flags;
559 unsigned int length, i;
560
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100561 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ch == '\t') {
563 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
564 raw->line_pos += length;
565 ch = ' ';
566 } else if (ch == '\n') {
567 length = 1;
568 raw->line_pos = 0;
569 } else {
570 length = 1;
571 raw->line_pos++;
572 }
573 raw3215_make_room(raw, length);
574
575 for (i = 0; i < length; i++) {
576 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
577 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
578 raw->count++;
579 }
580 if (!(raw->flags & RAW3215_WORKING)) {
581 raw3215_mk_write_req(raw);
582 /* start or queue request */
583 raw3215_try_io(raw);
584 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100585 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
589 * Flush routine, it simply sets the flush flag and tries to start
590 * pending IO.
591 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200592static void raw3215_flush_buffer(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 unsigned long flags;
595
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100596 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (raw->count > 0) {
598 raw->flags |= RAW3215_FLUSHING;
599 raw3215_try_io(raw);
600 raw->flags &= ~RAW3215_FLUSHING;
601 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100602 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
606 * Fire up a 3215 device.
607 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200608static int raw3215_startup(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 unsigned long flags;
611
612 if (raw->flags & RAW3215_ACTIVE)
613 return 0;
614 raw->line_pos = 0;
615 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100616 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100618 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return 0;
621}
622
623/*
624 * Shutdown a 3215 device.
625 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200626static void raw3215_shutdown(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 DECLARE_WAITQUEUE(wait, current);
629 unsigned long flags;
630
631 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
632 return;
633 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100634 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if ((raw->flags & RAW3215_WORKING) ||
636 raw->queued_write != NULL ||
637 raw->queued_read != NULL) {
638 raw->flags |= RAW3215_CLOSING;
639 add_wait_queue(&raw->empty_wait, &wait);
640 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100641 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100643 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 remove_wait_queue(&raw->empty_wait, &wait);
645 set_current_state(TASK_RUNNING);
646 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
647 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100648 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200651static int raw3215_probe (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct raw3215_info *raw;
654 int line;
655
Cornelia Hucka2e53802007-10-12 16:11:52 +0200656 /* Console is special. */
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700657 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
Cornelia Hucka2e53802007-10-12 16:11:52 +0200658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 raw = kmalloc(sizeof(struct raw3215_info) +
660 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
661 if (raw == NULL)
662 return -ENOMEM;
663
664 spin_lock(&raw3215_device_lock);
665 for (line = 0; line < NR_3215; line++) {
666 if (!raw3215[line]) {
667 raw3215[line] = raw;
668 break;
669 }
670 }
671 spin_unlock(&raw3215_device_lock);
672 if (line == NR_3215) {
673 kfree(raw);
674 return -ENODEV;
675 }
676
677 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
679 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800680 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 GFP_KERNEL|GFP_DMA);
682 if (raw->buffer == NULL) {
683 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200684 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 spin_unlock(&raw3215_device_lock);
686 kfree(raw);
687 return -ENOMEM;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 init_waitqueue_head(&raw->empty_wait);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100690 tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700692 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 cdev->handler = raw3215_irq;
694
695 return 0;
696}
697
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200698static void raw3215_remove (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct raw3215_info *raw;
701
702 ccw_device_set_offline(cdev);
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700703 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (raw) {
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700705 dev_set_drvdata(&cdev->dev, NULL);
Jesper Juhl17fd6822005-11-07 01:01:30 -0800706 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(raw);
708 }
709}
710
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200711static int raw3215_set_online (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct raw3215_info *raw;
714
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700715 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!raw)
717 return -ENODEV;
718
719 return raw3215_startup(raw);
720}
721
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200722static int raw3215_set_offline (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct raw3215_info *raw;
725
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700726 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!raw)
728 return -ENODEV;
729
730 raw3215_shutdown(raw);
731
732 return 0;
733}
734
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200735static int raw3215_pm_stop(struct ccw_device *cdev)
736{
737 struct raw3215_info *raw;
738 unsigned long flags;
739
740 /* Empty the output buffer, then prevent new I/O. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200741 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200742 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
743 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
744 raw->flags |= RAW3215_FROZEN;
745 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
746 return 0;
747}
748
749static int raw3215_pm_start(struct ccw_device *cdev)
750{
751 struct raw3215_info *raw;
752 unsigned long flags;
753
754 /* Allow I/O again and flush output buffer. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200755 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200756 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
757 raw->flags &= ~RAW3215_FROZEN;
758 raw->flags |= RAW3215_FLUSHING;
759 raw3215_try_io(raw);
760 raw->flags &= ~RAW3215_FLUSHING;
761 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
762 return 0;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static struct ccw_device_id raw3215_id[] = {
766 { CCW_DEVICE(0x3215, 0) },
767 { /* end of list */ },
768};
769
770static struct ccw_driver raw3215_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +0100771 .driver = {
772 .name = "3215",
773 .owner = THIS_MODULE,
774 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 .ids = raw3215_id,
776 .probe = &raw3215_probe,
777 .remove = &raw3215_remove,
778 .set_online = &raw3215_set_online,
779 .set_offline = &raw3215_set_offline,
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200780 .freeze = &raw3215_pm_stop,
781 .thaw = &raw3215_pm_start,
782 .restore = &raw3215_pm_start,
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100783 .int_class = IOINT_C15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784};
785
786#ifdef CONFIG_TN3215_CONSOLE
787/*
788 * Write a string to the 3215 console
789 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200790static void con3215_write(struct console *co, const char *str,
791 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct raw3215_info *raw;
794 int i;
795
796 if (count <= 0)
797 return;
798 raw = raw3215[0]; /* console 3215 is the first one */
799 while (count > 0) {
800 for (i = 0; i < count; i++)
801 if (str[i] == '\t' || str[i] == '\n')
802 break;
803 raw3215_write(raw, str, i);
804 count -= i;
805 str += i;
806 if (count > 0) {
807 raw3215_putchar(raw, *str);
808 count--;
809 str++;
810 }
811 }
812}
813
814static struct tty_driver *con3215_device(struct console *c, int *index)
815{
816 *index = c->index;
817 return tty3215_driver;
818}
819
820/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200821 * panic() calls con3215_flush through a panic_notifier
822 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200824static void con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct raw3215_info *raw;
827 unsigned long flags;
828
829 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200830 if (raw->flags & RAW3215_FROZEN)
831 /* The console is still frozen for suspend. */
832 if (ccw_device_force_console())
833 /* Forcing didn't work, no panic message .. */
834 return;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100835 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100837 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Holger Smolinski2332ce12008-10-10 21:33:27 +0200840static int con3215_notify(struct notifier_block *self,
841 unsigned long event, void *data)
842{
843 con3215_flush();
844 return NOTIFY_OK;
845}
846
847static struct notifier_block on_panic_nb = {
848 .notifier_call = con3215_notify,
849 .priority = 0,
850};
851
852static struct notifier_block on_reboot_nb = {
853 .notifier_call = con3215_notify,
854 .priority = 0,
855};
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/*
858 * The console structure for the 3215 console
859 */
860static struct console con3215 = {
861 .name = "ttyS",
862 .write = con3215_write,
863 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 .flags = CON_PRINTBUFFER,
865};
866
867/*
868 * 3215 console initialization code called from console_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200870static int __init con3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct ccw_device *cdev;
873 struct raw3215_info *raw;
874 struct raw3215_req *req;
875 int i;
876
877 /* Check if 3215 is to be the console */
878 if (!CONSOLE_IS_3215)
879 return -ENODEV;
880
881 /* Set the console mode for VM */
882 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700883 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
884 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
887 /* allocate 3215 request structures */
888 raw3215_freelist = NULL;
889 spin_lock_init(&raw3215_freelist_lock);
890 for (i = 0; i < NR_3215_REQ; i++) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200891 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 req->next = raw3215_freelist;
893 raw3215_freelist = req;
894 }
895
896 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800897 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -ENODEV;
899
900 raw3215[0] = raw = (struct raw3215_info *)
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200901 kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
902 raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
903 raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 raw->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700905 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 cdev->handler = raw3215_irq;
907
908 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 init_waitqueue_head(&raw->empty_wait);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100910 tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Request the console irq */
913 if (raw3215_startup(raw) != 0) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200914 kfree(raw->inbuf);
915 kfree(raw->buffer);
916 kfree(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -ENODEV;
919 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200920 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
921 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 register_console(&con3215);
923 return 0;
924}
925console_initcall(con3215_init);
926#endif
927
928/*
929 * tty3215_open
930 *
931 * This routine is called whenever a 3215 tty is opened.
932 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200933static int tty3215_open(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct raw3215_info *raw;
936 int retval, line;
937
938 line = tty->index;
939 if ((line < 0) || (line >= NR_3215))
940 return -ENODEV;
941
942 raw = raw3215[line];
943 if (raw == NULL)
944 return -ENODEV;
945
946 tty->driver_data = raw;
947 raw->tty = tty;
948
949 tty->low_latency = 0; /* don't use bottom half for pushing chars */
950 /*
951 * Start up 3215 device
952 */
953 retval = raw3215_startup(raw);
954 if (retval)
955 return retval;
956
957 return 0;
958}
959
960/*
961 * tty3215_close()
962 *
963 * This routine is called when the 3215 tty is closed. We wait
964 * for the remaining request to be completed. Then we clean up.
965 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200966static void tty3215_close(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct raw3215_info *raw;
969
970 raw = (struct raw3215_info *) tty->driver_data;
971 if (raw == NULL || tty->count > 1)
972 return;
973 tty->closing = 1;
974 /* Shutdown the terminal */
975 raw3215_shutdown(raw);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100976 tasklet_kill(&raw->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 tty->closing = 0;
978 raw->tty = NULL;
979}
980
981/*
982 * Returns the amount of free space in the output buffer.
983 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200984static int tty3215_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct raw3215_info *raw;
987
988 raw = (struct raw3215_info *) tty->driver_data;
989
990 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
991 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
992 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
993 else
994 return 0;
995}
996
997/*
998 * String write routine for 3215 ttys
999 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001000static int tty3215_write(struct tty_struct * tty,
1001 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct raw3215_info *raw;
1004
1005 if (!tty)
1006 return 0;
1007 raw = (struct raw3215_info *) tty->driver_data;
1008 raw3215_write(raw, buf, count);
1009 return count;
1010}
1011
1012/*
1013 * Put character routine for 3215 ttys
1014 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001015static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 struct raw3215_info *raw;
1018
1019 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -07001020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 raw = (struct raw3215_info *) tty->driver_data;
1022 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -07001023 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001026static void tty3215_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028}
1029
1030/*
1031 * Returns the number of characters in the output buffer
1032 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001033static int tty3215_chars_in_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 return raw->count;
1039}
1040
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001041static void tty3215_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct raw3215_info *raw;
1044
1045 raw = (struct raw3215_info *) tty->driver_data;
1046 raw3215_flush_buffer(raw);
1047 tty_wakeup(tty);
1048}
1049
1050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * Disable reading from a 3215 tty
1052 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001053static void tty3215_throttle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct raw3215_info *raw;
1056
1057 raw = (struct raw3215_info *) tty->driver_data;
1058 raw->flags |= RAW3215_THROTTLED;
1059}
1060
1061/*
1062 * Enable reading from a 3215 tty
1063 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001064static void tty3215_unthrottle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct raw3215_info *raw;
1067 unsigned long flags;
1068
1069 raw = (struct raw3215_info *) tty->driver_data;
1070 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001071 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 raw->flags &= ~RAW3215_THROTTLED;
1073 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001074 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076}
1077
1078/*
1079 * Disable writing to a 3215 tty
1080 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001081static void tty3215_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct raw3215_info *raw;
1084
1085 raw = (struct raw3215_info *) tty->driver_data;
1086 raw->flags |= RAW3215_STOPPED;
1087}
1088
1089/*
1090 * Enable writing to a 3215 tty
1091 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001092static void tty3215_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct raw3215_info *raw;
1095 unsigned long flags;
1096
1097 raw = (struct raw3215_info *) tty->driver_data;
1098 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001099 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 raw->flags &= ~RAW3215_STOPPED;
1101 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001102 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104}
1105
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001106static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 .open = tty3215_open,
1108 .close = tty3215_close,
1109 .write = tty3215_write,
1110 .put_char = tty3215_put_char,
1111 .flush_chars = tty3215_flush_chars,
1112 .write_room = tty3215_write_room,
1113 .chars_in_buffer = tty3215_chars_in_buffer,
1114 .flush_buffer = tty3215_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 .throttle = tty3215_throttle,
1116 .unthrottle = tty3215_unthrottle,
1117 .stop = tty3215_stop,
1118 .start = tty3215_start,
1119};
1120
1121/*
1122 * 3215 tty registration code called from tty_init().
1123 * Most kernel services (incl. kmalloc) are available at this poimt.
1124 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001125static int __init tty3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct tty_driver *driver;
1128 int ret;
1129
1130 if (!CONSOLE_IS_3215)
1131 return 0;
1132
1133 driver = alloc_tty_driver(NR_3215);
1134 if (!driver)
1135 return -ENOMEM;
1136
1137 ret = ccw_driver_register(&raw3215_ccw_driver);
1138 if (ret) {
1139 put_tty_driver(driver);
1140 return ret;
1141 }
1142 /*
1143 * Initialize the tty_driver structure
1144 * Entries in tty3215_driver that are NOT initialized:
1145 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1146 */
1147
1148 driver->owner = THIS_MODULE;
1149 driver->driver_name = "tty3215";
1150 driver->name = "ttyS";
1151 driver->major = TTY_MAJOR;
1152 driver->minor_start = 64;
1153 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1154 driver->subtype = SYSTEM_TYPE_TTY;
1155 driver->init_termios = tty_std_termios;
1156 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1157 driver->init_termios.c_oflag = ONLCR | XTABS;
1158 driver->init_termios.c_lflag = ISIG;
1159 driver->flags = TTY_DRIVER_REAL_RAW;
1160 tty_set_operations(driver, &tty3215_ops);
1161 ret = tty_register_driver(driver);
1162 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 put_tty_driver(driver);
1164 return ret;
1165 }
1166 tty3215_driver = driver;
1167 return 0;
1168}
1169
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001170static void __exit tty3215_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 tty_unregister_driver(tty3215_driver);
1173 put_tty_driver(tty3215_driver);
1174 ccw_driver_unregister(&raw3215_ccw_driver);
1175}
1176
1177module_init(tty3215_init);
1178module_exit(tty3215_exit);