blob: e928e04080017203f3806ab14176c7ea05ba10c2 [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>
Jiri Slaby8dd360f2012-04-11 11:14:58 +020023#include <linux/serial.h> /* ASYNC_* flags */
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
52#define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
53
54#define TAB_STOP_SIZE 8 /* tab stop size */
55
56/*
57 * Request types for a 3215 device
58 */
59enum raw3215_type {
60 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
61};
62
63/*
64 * Request structure for a 3215 device
65 */
66struct raw3215_req {
67 enum raw3215_type type; /* type of the request */
68 int start, len; /* start index & len in output buffer */
69 int delayable; /* indication to wait for more data */
70 int residual; /* residual count for read request */
71 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
72 struct raw3215_info *info; /* pointer to main structure */
73 struct raw3215_req *next; /* pointer to next request */
74} __attribute__ ((aligned(8)));
75
76struct raw3215_info {
Jiri Slaby8dd360f2012-04-11 11:14:58 +020077 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct ccw_device *cdev; /* device for tty driver */
79 spinlock_t *lock; /* pointer to irq lock */
80 int flags; /* state flags */
81 char *buffer; /* pointer to output buffer */
82 char *inbuf; /* pointer to input buffer */
83 int head; /* first free byte in output buffer */
84 int count; /* number of bytes in output buffer */
85 int written; /* number of bytes in write requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct raw3215_req *queued_read; /* pointer to queued read requests */
87 struct raw3215_req *queued_write;/* pointer to queued write requests */
Martin Schwidefsky656d9122012-02-17 10:29:22 +010088 struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 wait_queue_head_t empty_wait; /* wait queue for flushing */
90 struct timer_list timer; /* timer for delayed output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int line_pos; /* position on the line (for tabs) */
92 char ubuffer[80]; /* copy_from_user buffer */
93};
94
95/* array of 3215 devices structures */
96static struct raw3215_info *raw3215[NR_3215];
97/* spinlock to protect the raw3215 array */
98static DEFINE_SPINLOCK(raw3215_device_lock);
99/* list of free request structures */
100static struct raw3215_req *raw3215_freelist;
101/* spinlock to protect free list */
102static spinlock_t raw3215_freelist_lock;
103
104static struct tty_driver *tty3215_driver;
105
106/*
107 * Get a request structure from the free list
108 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200109static inline struct raw3215_req *raw3215_alloc_req(void)
110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct raw3215_req *req;
112 unsigned long flags;
113
114 spin_lock_irqsave(&raw3215_freelist_lock, flags);
115 req = raw3215_freelist;
116 raw3215_freelist = req->next;
117 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
118 return req;
119}
120
121/*
122 * Put a request structure back to the free list
123 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200124static inline void raw3215_free_req(struct raw3215_req *req)
125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long flags;
127
128 if (req->type == RAW3215_FREE)
129 return; /* don't free a free request */
130 req->type = RAW3215_FREE;
131 spin_lock_irqsave(&raw3215_freelist_lock, flags);
132 req->next = raw3215_freelist;
133 raw3215_freelist = req;
134 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
135}
136
137/*
138 * Set up a read request that reads up to 160 byte from the 3215 device.
139 * If there is a queued read request it is used, but that shouldn't happen
140 * because a 3215 terminal won't accept a new read before the old one is
141 * completed.
142 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200143static void raw3215_mk_read_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct raw3215_req *req;
146 struct ccw1 *ccw;
147
148 /* there can only be ONE read request at a time */
149 req = raw->queued_read;
150 if (req == NULL) {
151 /* no queued read request, use new req structure */
152 req = raw3215_alloc_req();
153 req->type = RAW3215_READ;
154 req->info = raw;
155 raw->queued_read = req;
156 }
157
158 ccw = req->ccws;
159 ccw->cmd_code = 0x0A; /* read inquiry */
160 ccw->flags = 0x20; /* ignore incorrect length */
161 ccw->count = 160;
162 ccw->cda = (__u32) __pa(raw->inbuf);
163}
164
165/*
166 * Set up a write request with the information from the main structure.
167 * A ccw chain is created that writes as much as possible from the output
168 * buffer to the 3215 device. If a queued write exists it is replaced by
169 * the new, probably lengthened request.
170 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200171static void raw3215_mk_write_req(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct raw3215_req *req;
174 struct ccw1 *ccw;
175 int len, count, ix, lines;
176
177 if (raw->count <= raw->written)
178 return;
179 /* check if there is a queued write request */
180 req = raw->queued_write;
181 if (req == NULL) {
182 /* no queued write request, use new req structure */
183 req = raw3215_alloc_req();
184 req->type = RAW3215_WRITE;
185 req->info = raw;
186 raw->queued_write = req;
187 } else {
188 raw->written -= req->len;
189 }
190
191 ccw = req->ccws;
192 req->start = (raw->head - raw->count + raw->written) &
193 (RAW3215_BUFFER_SIZE - 1);
194 /*
195 * now we have to count newlines. We can at max accept
196 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
197 * a restriction in VM
198 */
199 lines = 0;
200 ix = req->start;
201 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
202 if (raw->buffer[ix] == 0x15)
203 lines++;
204 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
205 }
206 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
207 if (len > RAW3215_MAX_BYTES)
208 len = RAW3215_MAX_BYTES;
209 req->len = len;
210 raw->written += len;
211
212 /* set the indication if we should try to enlarge this request */
213 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
214
215 ix = req->start;
216 while (len > 0) {
217 if (ccw > req->ccws)
218 ccw[-1].flags |= 0x40; /* use command chaining */
219 ccw->cmd_code = 0x01; /* write, auto carrier return */
220 ccw->flags = 0x20; /* ignore incorrect length ind. */
221 ccw->cda =
222 (__u32) __pa(raw->buffer + ix);
223 count = len;
224 if (ix + count > RAW3215_BUFFER_SIZE)
225 count = RAW3215_BUFFER_SIZE - ix;
226 ccw->count = count;
227 len -= count;
228 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
229 ccw++;
230 }
231 /*
232 * Add a NOP to the channel program. 3215 devices are purely
233 * emulated and its much better to avoid the channel end
234 * interrupt in this case.
235 */
236 if (ccw > req->ccws)
237 ccw[-1].flags |= 0x40; /* use command chaining */
238 ccw->cmd_code = 0x03; /* NOP */
239 ccw->flags = 0;
240 ccw->cda = 0;
241 ccw->count = 1;
242}
243
244/*
245 * Start a read or a write request
246 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200247static void raw3215_start_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct raw3215_req *req;
250 int res;
251
252 req = raw->queued_read;
253 if (req != NULL &&
254 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
255 /* dequeue request */
256 raw->queued_read = NULL;
257 res = ccw_device_start(raw->cdev, req->ccws,
258 (unsigned long) req, 0, 0);
259 if (res != 0) {
260 /* do_IO failed, put request back to queue */
261 raw->queued_read = req;
262 } else {
263 raw->flags |= RAW3215_WORKING;
264 }
265 }
266 req = raw->queued_write;
267 if (req != NULL &&
268 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
269 /* dequeue request */
270 raw->queued_write = NULL;
271 res = ccw_device_start(raw->cdev, req->ccws,
272 (unsigned long) req, 0, 0);
273 if (res != 0) {
274 /* do_IO failed, put request back to queue */
275 raw->queued_write = req;
276 } else {
277 raw->flags |= RAW3215_WORKING;
278 }
279 }
280}
281
282/*
283 * Function to start a delayed output after RAW3215_TIMEOUT seconds
284 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200285static void raw3215_timeout(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct raw3215_info *raw = (struct raw3215_info *) __data;
288 unsigned long flags;
289
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100290 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (raw->flags & RAW3215_TIMER_RUNS) {
292 del_timer(&raw->timer);
293 raw->flags &= ~RAW3215_TIMER_RUNS;
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200294 if (!(raw->port.flags & ASYNC_SUSPENDED)) {
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200295 raw3215_mk_write_req(raw);
296 raw3215_start_io(raw);
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100299 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
303 * Function to conditionally start an IO. A read is started immediately,
304 * a write is only started immediately if the flush flag is on or the
305 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
306 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
307 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200308static inline void raw3215_try_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200310 if (!(raw->port.flags & ASYNC_INITIALIZED) ||
311 (raw->port.flags & ASYNC_SUSPENDED))
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 add_timer(&raw->timer);
328 raw->flags |= RAW3215_TIMER_RUNS;
329 }
330 }
331}
332
333/*
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100334 * Call tty_wakeup from tasklet context
335 */
336static void raw3215_wakeup(unsigned long data)
337{
338 struct raw3215_info *raw = (struct raw3215_info *) data;
339 tty_wakeup(raw->tty);
340}
341
342/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200343 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
Jiri Slaby86b260072012-04-11 11:14:59 +0200345static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 raw3215_mk_write_req(raw);
348 raw3215_try_io(raw);
Jiri Slaby86b260072012-04-11 11:14:59 +0200349 if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100350 tasklet_schedule(&raw->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700365 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 req = (struct raw3215_req *) intparm;
Jiri Slaby86b260072012-04-11 11:14:59 +0200367 tty = tty_port_tty_get(&raw->port);
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)
Jiri Slaby86b260072012-04-11 11:14:59 +0200371 raw3215_next_io(raw, tty);
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);
Jiri Slaby86b260072012-04-11 11:14:59 +0200381 raw3215_next_io(raw, tty);
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)
Jiri Slaby86b260072012-04-11 11:14:59 +0200387 goto put_tty; /* That shouldn't happen ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 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)
Jiri Slaby86b260072012-04-11 11:14:59 +0200397 goto put_tty; /* That shouldn't happen ... */
398 if (req->type == RAW3215_READ && tty != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unsigned int cchar;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 EBCASC(raw->inbuf, count);
403 cchar = ctrlchar_handle(raw->inbuf, count, tty);
404 switch (cchar & CTRLCHAR_MASK) {
405 case CTRLCHAR_SYSRQ:
406 break;
407
408 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800409 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Jiri Slaby86b260072012-04-11 11:14:59 +0200410 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412
413 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800415 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
416 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
417 /* add the auto \n */
418 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 count++;
420 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800421 count -= 2;
422 tty_insert_flip_string(tty, raw->inbuf, count);
Jiri Slaby86b260072012-04-11 11:14:59 +0200423 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 }
426 } else if (req->type == RAW3215_WRITE) {
427 raw->count -= req->len;
428 raw->written -= req->len;
429 }
430 raw->flags &= ~RAW3215_WORKING;
431 raw3215_free_req(req);
432 /* check for empty wait */
433 if (waitqueue_active(&raw->empty_wait) &&
434 raw->queued_write == NULL &&
435 raw->queued_read == NULL) {
436 wake_up_interruptible(&raw->empty_wait);
437 }
Jiri Slaby86b260072012-04-11 11:14:59 +0200438 raw3215_next_io(raw, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 default:
441 /* Strange interrupt, I'll do my best to clean up */
442 if (req != NULL && req->type != RAW3215_FREE) {
443 if (req->type == RAW3215_WRITE) {
444 raw->count -= req->len;
445 raw->written -= req->len;
446 }
447 raw->flags &= ~RAW3215_WORKING;
448 raw3215_free_req(req);
449 }
Jiri Slaby86b260072012-04-11 11:14:59 +0200450 raw3215_next_io(raw, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Jiri Slaby86b260072012-04-11 11:14:59 +0200452put_tty:
453 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
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. */
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200487 if (raw->port.flags & ASYNC_SUSPENDED) {
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200488 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
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200609 if (raw->port.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
611 raw->line_pos = 0;
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200612 raw->port.flags |= ASYNC_INITIALIZED;
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
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200628 if (!(raw->port.flags & ASYNC_INITIALIZED) ||
629 (raw->flags & RAW3215_FIXED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return;
631 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100632 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if ((raw->flags & RAW3215_WORKING) ||
634 raw->queued_write != NULL ||
635 raw->queued_read != NULL) {
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200636 raw->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 add_wait_queue(&raw->empty_wait, &wait);
638 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100639 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100641 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 remove_wait_queue(&raw->empty_wait, &wait);
643 set_current_state(TASK_RUNNING);
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200644 raw->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100646 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200649static struct raw3215_info *raw3215_alloc_info(void)
650{
651 struct raw3215_info *info;
652
653 info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
654 if (!info)
655 return NULL;
656
657 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
658 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
659 if (!info->buffer || !info->inbuf) {
660 kfree(info);
661 return NULL;
662 }
663
664 setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
665 init_waitqueue_head(&info->empty_wait);
666 tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200667 tty_port_init(&info->port);
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200668
669 return info;
670}
671
672static void raw3215_free_info(struct raw3215_info *raw)
673{
674 kfree(raw->inbuf);
675 kfree(raw->buffer);
676 kfree(raw);
677}
678
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200679static int raw3215_probe (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct raw3215_info *raw;
682 int line;
683
Cornelia Hucka2e53802007-10-12 16:11:52 +0200684 /* Console is special. */
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700685 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
Cornelia Hucka2e53802007-10-12 16:11:52 +0200686 return 0;
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200687
688 raw = raw3215_alloc_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (raw == NULL)
690 return -ENOMEM;
691
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200692 raw->cdev = cdev;
693 dev_set_drvdata(&cdev->dev, raw);
694 cdev->handler = raw3215_irq;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_lock(&raw3215_device_lock);
697 for (line = 0; line < NR_3215; line++) {
698 if (!raw3215[line]) {
699 raw3215[line] = raw;
700 break;
701 }
702 }
703 spin_unlock(&raw3215_device_lock);
704 if (line == NR_3215) {
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200705 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ENODEV;
707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200712static void raw3215_remove (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct raw3215_info *raw;
715
716 ccw_device_set_offline(cdev);
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700717 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (raw) {
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700719 dev_set_drvdata(&cdev->dev, NULL);
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200720 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722}
723
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200724static int raw3215_set_online (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct raw3215_info *raw;
727
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700728 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!raw)
730 return -ENODEV;
731
732 return raw3215_startup(raw);
733}
734
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200735static int raw3215_set_offline (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct raw3215_info *raw;
738
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700739 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!raw)
741 return -ENODEV;
742
743 raw3215_shutdown(raw);
744
745 return 0;
746}
747
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200748static int raw3215_pm_stop(struct ccw_device *cdev)
749{
750 struct raw3215_info *raw;
751 unsigned long flags;
752
753 /* Empty the output buffer, then prevent new I/O. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200754 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200755 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
756 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200757 raw->port.flags |= ASYNC_SUSPENDED;
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200758 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
759 return 0;
760}
761
762static int raw3215_pm_start(struct ccw_device *cdev)
763{
764 struct raw3215_info *raw;
765 unsigned long flags;
766
767 /* Allow I/O again and flush output buffer. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200768 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200769 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200770 raw->port.flags &= ~ASYNC_SUSPENDED;
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200771 raw->flags |= RAW3215_FLUSHING;
772 raw3215_try_io(raw);
773 raw->flags &= ~RAW3215_FLUSHING;
774 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
775 return 0;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static struct ccw_device_id raw3215_id[] = {
779 { CCW_DEVICE(0x3215, 0) },
780 { /* end of list */ },
781};
782
783static struct ccw_driver raw3215_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +0100784 .driver = {
785 .name = "3215",
786 .owner = THIS_MODULE,
787 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .ids = raw3215_id,
789 .probe = &raw3215_probe,
790 .remove = &raw3215_remove,
791 .set_online = &raw3215_set_online,
792 .set_offline = &raw3215_set_offline,
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200793 .freeze = &raw3215_pm_stop,
794 .thaw = &raw3215_pm_start,
795 .restore = &raw3215_pm_start,
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100796 .int_class = IOINT_C15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797};
798
799#ifdef CONFIG_TN3215_CONSOLE
800/*
801 * Write a string to the 3215 console
802 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200803static void con3215_write(struct console *co, const char *str,
804 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct raw3215_info *raw;
807 int i;
808
809 if (count <= 0)
810 return;
811 raw = raw3215[0]; /* console 3215 is the first one */
812 while (count > 0) {
813 for (i = 0; i < count; i++)
814 if (str[i] == '\t' || str[i] == '\n')
815 break;
816 raw3215_write(raw, str, i);
817 count -= i;
818 str += i;
819 if (count > 0) {
820 raw3215_putchar(raw, *str);
821 count--;
822 str++;
823 }
824 }
825}
826
827static struct tty_driver *con3215_device(struct console *c, int *index)
828{
829 *index = c->index;
830 return tty3215_driver;
831}
832
833/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200834 * panic() calls con3215_flush through a panic_notifier
835 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200837static void con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct raw3215_info *raw;
840 unsigned long flags;
841
842 raw = raw3215[0]; /* console 3215 is the first one */
Jiri Slaby8dd360f2012-04-11 11:14:58 +0200843 if (raw->port.flags & ASYNC_SUSPENDED)
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200844 /* The console is still frozen for suspend. */
845 if (ccw_device_force_console())
846 /* Forcing didn't work, no panic message .. */
847 return;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100848 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100850 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Holger Smolinski2332ce12008-10-10 21:33:27 +0200853static int con3215_notify(struct notifier_block *self,
854 unsigned long event, void *data)
855{
856 con3215_flush();
857 return NOTIFY_OK;
858}
859
860static struct notifier_block on_panic_nb = {
861 .notifier_call = con3215_notify,
862 .priority = 0,
863};
864
865static struct notifier_block on_reboot_nb = {
866 .notifier_call = con3215_notify,
867 .priority = 0,
868};
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/*
871 * The console structure for the 3215 console
872 */
873static struct console con3215 = {
874 .name = "ttyS",
875 .write = con3215_write,
876 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .flags = CON_PRINTBUFFER,
878};
879
880/*
881 * 3215 console initialization code called from console_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200883static int __init con3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct ccw_device *cdev;
886 struct raw3215_info *raw;
887 struct raw3215_req *req;
888 int i;
889
890 /* Check if 3215 is to be the console */
891 if (!CONSOLE_IS_3215)
892 return -ENODEV;
893
894 /* Set the console mode for VM */
895 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700896 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
897 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 /* allocate 3215 request structures */
901 raw3215_freelist = NULL;
902 spin_lock_init(&raw3215_freelist_lock);
903 for (i = 0; i < NR_3215_REQ; i++) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200904 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 req->next = raw3215_freelist;
906 raw3215_freelist = req;
907 }
908
909 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800910 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENODEV;
912
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200913 raw3215[0] = raw = raw3215_alloc_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 raw->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700915 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 cdev->handler = raw3215_irq;
917
918 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* Request the console irq */
921 if (raw3215_startup(raw) != 0) {
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200922 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -ENODEV;
925 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200926 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
927 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 register_console(&con3215);
929 return 0;
930}
931console_initcall(con3215_init);
932#endif
933
934/*
935 * tty3215_open
936 *
937 * This routine is called whenever a 3215 tty is opened.
938 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200939static int tty3215_open(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct raw3215_info *raw;
Jiri Slaby410235f2012-03-05 14:52:01 +0100942 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Jiri Slaby410235f2012-03-05 14:52:01 +0100944 raw = raw3215[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (raw == NULL)
946 return -ENODEV;
947
948 tty->driver_data = raw;
Jiri Slaby86b260072012-04-11 11:14:59 +0200949 tty_port_tty_set(&raw->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 tty->low_latency = 0; /* don't use bottom half for pushing chars */
952 /*
953 * Start up 3215 device
954 */
955 retval = raw3215_startup(raw);
956 if (retval)
957 return retval;
958
959 return 0;
960}
961
962/*
963 * tty3215_close()
964 *
965 * This routine is called when the 3215 tty is closed. We wait
966 * for the remaining request to be completed. Then we clean up.
967 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200968static void tty3215_close(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct raw3215_info *raw;
971
972 raw = (struct raw3215_info *) tty->driver_data;
973 if (raw == NULL || tty->count > 1)
974 return;
975 tty->closing = 1;
976 /* Shutdown the terminal */
977 raw3215_shutdown(raw);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100978 tasklet_kill(&raw->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 tty->closing = 0;
Jiri Slaby86b260072012-04-11 11:14:59 +0200980 tty_port_tty_set(&raw->port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Returns the amount of free space in the output buffer.
985 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200986static int tty3215_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct raw3215_info *raw;
989
990 raw = (struct raw3215_info *) tty->driver_data;
991
992 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
993 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
994 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
995 else
996 return 0;
997}
998
999/*
1000 * String write routine for 3215 ttys
1001 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001002static int tty3215_write(struct tty_struct * tty,
1003 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct raw3215_info *raw;
1006
1007 if (!tty)
1008 return 0;
1009 raw = (struct raw3215_info *) tty->driver_data;
1010 raw3215_write(raw, buf, count);
1011 return count;
1012}
1013
1014/*
1015 * Put character routine for 3215 ttys
1016 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001017static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct raw3215_info *raw;
1020
1021 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -07001022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 raw = (struct raw3215_info *) tty->driver_data;
1024 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -07001025 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001028static void tty3215_flush_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030}
1031
1032/*
1033 * Returns the number of characters in the output buffer
1034 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001035static int tty3215_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct raw3215_info *raw;
1038
1039 raw = (struct raw3215_info *) tty->driver_data;
1040 return raw->count;
1041}
1042
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001043static void tty3215_flush_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct raw3215_info *raw;
1046
1047 raw = (struct raw3215_info *) tty->driver_data;
1048 raw3215_flush_buffer(raw);
1049 tty_wakeup(tty);
1050}
1051
1052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 * Disable reading from a 3215 tty
1054 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001055static void tty3215_throttle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct raw3215_info *raw;
1058
1059 raw = (struct raw3215_info *) tty->driver_data;
1060 raw->flags |= RAW3215_THROTTLED;
1061}
1062
1063/*
1064 * Enable reading from a 3215 tty
1065 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001066static void tty3215_unthrottle(struct tty_struct * tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct raw3215_info *raw;
1069 unsigned long flags;
1070
1071 raw = (struct raw3215_info *) tty->driver_data;
1072 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001073 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 raw->flags &= ~RAW3215_THROTTLED;
1075 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001076 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078}
1079
1080/*
1081 * Disable writing to a 3215 tty
1082 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001083static void tty3215_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct raw3215_info *raw;
1086
1087 raw = (struct raw3215_info *) tty->driver_data;
1088 raw->flags |= RAW3215_STOPPED;
1089}
1090
1091/*
1092 * Enable writing to a 3215 tty
1093 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001094static void tty3215_start(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct raw3215_info *raw;
1097 unsigned long flags;
1098
1099 raw = (struct raw3215_info *) tty->driver_data;
1100 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001101 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 raw->flags &= ~RAW3215_STOPPED;
1103 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001104 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001108static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 .open = tty3215_open,
1110 .close = tty3215_close,
1111 .write = tty3215_write,
1112 .put_char = tty3215_put_char,
1113 .flush_chars = tty3215_flush_chars,
1114 .write_room = tty3215_write_room,
1115 .chars_in_buffer = tty3215_chars_in_buffer,
1116 .flush_buffer = tty3215_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 .throttle = tty3215_throttle,
1118 .unthrottle = tty3215_unthrottle,
1119 .stop = tty3215_stop,
1120 .start = tty3215_start,
1121};
1122
1123/*
1124 * 3215 tty registration code called from tty_init().
1125 * Most kernel services (incl. kmalloc) are available at this poimt.
1126 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001127static int __init tty3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct tty_driver *driver;
1130 int ret;
1131
1132 if (!CONSOLE_IS_3215)
1133 return 0;
1134
1135 driver = alloc_tty_driver(NR_3215);
1136 if (!driver)
1137 return -ENOMEM;
1138
1139 ret = ccw_driver_register(&raw3215_ccw_driver);
1140 if (ret) {
1141 put_tty_driver(driver);
1142 return ret;
1143 }
1144 /*
1145 * Initialize the tty_driver structure
1146 * Entries in tty3215_driver that are NOT initialized:
1147 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1148 */
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 driver->driver_name = "tty3215";
1151 driver->name = "ttyS";
1152 driver->major = TTY_MAJOR;
1153 driver->minor_start = 64;
1154 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1155 driver->subtype = SYSTEM_TYPE_TTY;
1156 driver->init_termios = tty_std_termios;
1157 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1158 driver->init_termios.c_oflag = ONLCR | XTABS;
1159 driver->init_termios.c_lflag = ISIG;
1160 driver->flags = TTY_DRIVER_REAL_RAW;
1161 tty_set_operations(driver, &tty3215_ops);
1162 ret = tty_register_driver(driver);
1163 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 put_tty_driver(driver);
1165 return ret;
1166 }
1167 tty3215_driver = driver;
1168 return 0;
1169}
1170
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001171static void __exit tty3215_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 tty_unregister_driver(tty3215_driver);
1174 put_tty_driver(tty3215_driver);
1175 ccw_driver_unregister(&raw3215_ccw_driver);
1176}
1177
1178module_init(tty3215_init);
1179module_exit(tty3215_exit);