blob: 7e30f85ee3a5f38faff20583b3b665848bcafbc6 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 add_timer(&raw->timer);
329 raw->flags |= RAW3215_TIMER_RUNS;
330 }
331 }
332}
333
334/*
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100335 * Call tty_wakeup from tasklet context
336 */
337static void raw3215_wakeup(unsigned long data)
338{
339 struct raw3215_info *raw = (struct raw3215_info *) data;
340 tty_wakeup(raw->tty);
341}
342
343/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200344 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Heiko Carstens408aec32008-10-10 21:33:28 +0200346static void raw3215_next_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 raw3215_mk_write_req(raw);
349 raw3215_try_io(raw);
Martin Schwidefsky656d9122012-02-17 10:29:22 +0100350 if (raw->tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
351 tasklet_schedule(&raw->tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/*
355 * Interrupt routine, called from common io layer
356 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200357static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
358 struct irb *irb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct raw3215_info *raw;
361 struct raw3215_req *req;
362 struct tty_struct *tty;
363 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200364 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
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
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200648static struct raw3215_info *raw3215_alloc_info(void)
649{
650 struct raw3215_info *info;
651
652 info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
653 if (!info)
654 return NULL;
655
656 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
657 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
658 if (!info->buffer || !info->inbuf) {
659 kfree(info);
660 return NULL;
661 }
662
663 setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
664 init_waitqueue_head(&info->empty_wait);
665 tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
666
667 return info;
668}
669
670static void raw3215_free_info(struct raw3215_info *raw)
671{
672 kfree(raw->inbuf);
673 kfree(raw->buffer);
674 kfree(raw);
675}
676
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200677static int raw3215_probe (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct raw3215_info *raw;
680 int line;
681
Cornelia Hucka2e53802007-10-12 16:11:52 +0200682 /* Console is special. */
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700683 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
Cornelia Hucka2e53802007-10-12 16:11:52 +0200684 return 0;
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200685
686 raw = raw3215_alloc_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (raw == NULL)
688 return -ENOMEM;
689
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200690 raw->cdev = cdev;
691 dev_set_drvdata(&cdev->dev, raw);
692 cdev->handler = raw3215_irq;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock(&raw3215_device_lock);
695 for (line = 0; line < NR_3215; line++) {
696 if (!raw3215[line]) {
697 raw3215[line] = raw;
698 break;
699 }
700 }
701 spin_unlock(&raw3215_device_lock);
702 if (line == NR_3215) {
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200703 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -ENODEV;
705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200710static void raw3215_remove (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct raw3215_info *raw;
713
714 ccw_device_set_offline(cdev);
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) {
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700717 dev_set_drvdata(&cdev->dev, NULL);
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200718 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720}
721
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200722static int raw3215_set_online (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 return raw3215_startup(raw);
731}
732
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200733static int raw3215_set_offline (struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct raw3215_info *raw;
736
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700737 raw = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!raw)
739 return -ENODEV;
740
741 raw3215_shutdown(raw);
742
743 return 0;
744}
745
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200746static int raw3215_pm_stop(struct ccw_device *cdev)
747{
748 struct raw3215_info *raw;
749 unsigned long flags;
750
751 /* Empty the output buffer, then prevent new I/O. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200752 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200753 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
754 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
755 raw->flags |= RAW3215_FROZEN;
756 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
757 return 0;
758}
759
760static int raw3215_pm_start(struct ccw_device *cdev)
761{
762 struct raw3215_info *raw;
763 unsigned long flags;
764
765 /* Allow I/O again and flush output buffer. */
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200766 raw = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200767 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
768 raw->flags &= ~RAW3215_FROZEN;
769 raw->flags |= RAW3215_FLUSHING;
770 raw3215_try_io(raw);
771 raw->flags &= ~RAW3215_FLUSHING;
772 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
773 return 0;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static struct ccw_device_id raw3215_id[] = {
777 { CCW_DEVICE(0x3215, 0) },
778 { /* end of list */ },
779};
780
781static struct ccw_driver raw3215_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +0100782 .driver = {
783 .name = "3215",
784 .owner = THIS_MODULE,
785 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .ids = raw3215_id,
787 .probe = &raw3215_probe,
788 .remove = &raw3215_remove,
789 .set_online = &raw3215_set_online,
790 .set_offline = &raw3215_set_offline,
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200791 .freeze = &raw3215_pm_stop,
792 .thaw = &raw3215_pm_start,
793 .restore = &raw3215_pm_start,
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100794 .int_class = IOINT_C15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
797#ifdef CONFIG_TN3215_CONSOLE
798/*
799 * Write a string to the 3215 console
800 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200801static void con3215_write(struct console *co, const char *str,
802 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct raw3215_info *raw;
805 int i;
806
807 if (count <= 0)
808 return;
809 raw = raw3215[0]; /* console 3215 is the first one */
810 while (count > 0) {
811 for (i = 0; i < count; i++)
812 if (str[i] == '\t' || str[i] == '\n')
813 break;
814 raw3215_write(raw, str, i);
815 count -= i;
816 str += i;
817 if (count > 0) {
818 raw3215_putchar(raw, *str);
819 count--;
820 str++;
821 }
822 }
823}
824
825static struct tty_driver *con3215_device(struct console *c, int *index)
826{
827 *index = c->index;
828 return tty3215_driver;
829}
830
831/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200832 * panic() calls con3215_flush through a panic_notifier
833 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200835static void con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct raw3215_info *raw;
838 unsigned long flags;
839
840 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200841 if (raw->flags & RAW3215_FROZEN)
842 /* The console is still frozen for suspend. */
843 if (ccw_device_force_console())
844 /* Forcing didn't work, no panic message .. */
845 return;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100846 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100848 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Holger Smolinski2332ce12008-10-10 21:33:27 +0200851static int con3215_notify(struct notifier_block *self,
852 unsigned long event, void *data)
853{
854 con3215_flush();
855 return NOTIFY_OK;
856}
857
858static struct notifier_block on_panic_nb = {
859 .notifier_call = con3215_notify,
860 .priority = 0,
861};
862
863static struct notifier_block on_reboot_nb = {
864 .notifier_call = con3215_notify,
865 .priority = 0,
866};
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * The console structure for the 3215 console
870 */
871static struct console con3215 = {
872 .name = "ttyS",
873 .write = con3215_write,
874 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .flags = CON_PRINTBUFFER,
876};
877
878/*
879 * 3215 console initialization code called from console_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200881static int __init con3215_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct ccw_device *cdev;
884 struct raw3215_info *raw;
885 struct raw3215_req *req;
886 int i;
887
888 /* Check if 3215 is to be the console */
889 if (!CONSOLE_IS_3215)
890 return -ENODEV;
891
892 /* Set the console mode for VM */
893 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700894 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
895 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 /* allocate 3215 request structures */
899 raw3215_freelist = NULL;
900 spin_lock_init(&raw3215_freelist_lock);
901 for (i = 0; i < NR_3215_REQ; i++) {
Heiko Carstens6d56eee2009-06-22 12:08:04 +0200902 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 req->next = raw3215_freelist;
904 raw3215_freelist = req;
905 }
906
907 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800908 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ENODEV;
910
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200911 raw3215[0] = raw = raw3215_alloc_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 raw->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700913 dev_set_drvdata(&cdev->dev, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 cdev->handler = raw3215_irq;
915
916 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* Request the console irq */
919 if (raw3215_startup(raw) != 0) {
Jiri Slabyfe2fc9c2012-04-02 13:54:08 +0200920 raw3215_free_info(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENODEV;
923 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200924 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
925 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 register_console(&con3215);
927 return 0;
928}
929console_initcall(con3215_init);
930#endif
931
932/*
933 * tty3215_open
934 *
935 * This routine is called whenever a 3215 tty is opened.
936 */
Martin Schwidefsky77812a22009-06-16 10:30:29 +0200937static int tty3215_open(struct tty_struct *tty, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct raw3215_info *raw;
Jiri Slaby410235f2012-03-05 14:52:01 +0100940 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jiri Slaby410235f2012-03-05 14:52:01 +0100942 raw = raw3215[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 driver->driver_name = "tty3215";
1149 driver->name = "ttyS";
1150 driver->major = TTY_MAJOR;
1151 driver->minor_start = 64;
1152 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1153 driver->subtype = SYSTEM_TYPE_TTY;
1154 driver->init_termios = tty_std_termios;
1155 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1156 driver->init_termios.c_oflag = ONLCR | XTABS;
1157 driver->init_termios.c_lflag = ISIG;
1158 driver->flags = TTY_DRIVER_REAL_RAW;
1159 tty_set_operations(driver, &tty3215_ops);
1160 ret = tty_register_driver(driver);
1161 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 put_tty_driver(driver);
1163 return ret;
1164 }
1165 tty3215_driver = driver;
1166 return 0;
1167}
1168
Martin Schwidefsky77812a22009-06-16 10:30:29 +02001169static void __exit tty3215_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 tty_unregister_driver(tty3215_driver);
1172 put_tty_driver(tty3215_driver);
1173 ccw_driver_unregister(&raw3215_ccw_driver);
1174}
1175
1176module_init(tty3215_init);
1177module_exit(tty3215_exit);