blob: 5aaaa2ec8df44f4547c70b9f80d5c628c6d9d591 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Michael Holzheu62b749422009-06-16 10:30:40 +02002 * SCLP VT220 terminal driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Michael Holzheu62b749422009-06-16 10:30:40 +02004 * Copyright IBM Corp. 2003, 2009
5 *
6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/spinlock.h>
11#include <linux/list.h>
12#include <linux/wait.h>
13#include <linux/timer.h>
14#include <linux/kernel.h>
15#include <linux/tty.h>
16#include <linux/tty_driver.h>
Alan Cox33f0f882006-01-09 20:54:13 -080017#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/major.h>
21#include <linux/console.h>
22#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/interrupt.h>
24#include <linux/init.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020025#include <linux/reboot.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Holger Smolinski2332ce12008-10-10 21:33:27 +020027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
29#include "sclp.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define SCLP_VT220_MAJOR TTY_MAJOR
32#define SCLP_VT220_MINOR 65
33#define SCLP_VT220_DRIVER_NAME "sclp_vt220"
34#define SCLP_VT220_DEVICE_NAME "ttysclp"
35#define SCLP_VT220_CONSOLE_NAME "ttyS"
36#define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Representation of a single write request */
39struct sclp_vt220_request {
40 struct list_head list;
41 struct sclp_req sclp_req;
42 int retry_count;
43};
44
45/* VT220 SCCB */
46struct sclp_vt220_sccb {
47 struct sccb_header header;
48 struct evbuf_header evbuf;
49};
50
51#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb))
54
55/* Structures and data needed to register tty driver */
56static struct tty_driver *sclp_vt220_driver;
57
Jiri Slaby092f7372012-04-02 13:54:13 +020058static struct tty_port sclp_vt220_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Lock to protect internal data from concurrent access */
61static spinlock_t sclp_vt220_lock;
62
63/* List of empty pages to be used as write request buffers */
64static struct list_head sclp_vt220_empty;
65
66/* List of pending requests */
67static struct list_head sclp_vt220_outqueue;
68
Michael Holzheu62b749422009-06-16 10:30:40 +020069/* Suspend mode flag */
70static int sclp_vt220_suspended;
71
72/* Flag that output queue is currently running */
73static int sclp_vt220_queue_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Timer used for delaying write requests to merge subsequent messages into
76 * a single buffer */
77static struct timer_list sclp_vt220_timer;
78
79/* Pointer to current request buffer which has been partially filled but not
80 * yet sent */
81static struct sclp_vt220_request *sclp_vt220_current_request;
82
83/* Number of characters in current request buffer */
84static int sclp_vt220_buffered_chars;
85
Peter Oberparleiterad211792008-07-14 09:59:07 +020086/* Counter controlling core driver initialization. */
87static int __initdata sclp_vt220_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* Flag indicating that sclp_vt220_current_request should really
90 * have been already queued but wasn't because the SCLP was processing
91 * another buffer */
92static int sclp_vt220_flush_later;
93
94static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
Michael Holzheu62b749422009-06-16 10:30:40 +020095static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
96 enum sclp_pm_event sclp_pm_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int __sclp_vt220_emit(struct sclp_vt220_request *request);
98static void sclp_vt220_emit_current(void);
99
100/* Registration structure for our interest in SCLP event buffers */
101static struct sclp_register sclp_vt220_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200102 .send_mask = EVTYP_VT220MSG_MASK,
103 .receive_mask = EVTYP_VT220MSG_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .state_change_fn = NULL,
Michael Holzheu62b749422009-06-16 10:30:40 +0200105 .receiver_fn = sclp_vt220_receiver_fn,
106 .pm_event_fn = sclp_vt220_pm_event_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109
110/*
111 * Put provided request buffer back into queue and check emit pending
112 * buffers if necessary.
113 */
114static void
115sclp_vt220_process_queue(struct sclp_vt220_request *request)
116{
117 unsigned long flags;
118 void *page;
119
120 do {
121 /* Put buffer back to list of empty buffers */
122 page = request->sclp_req.sccb;
123 spin_lock_irqsave(&sclp_vt220_lock, flags);
124 /* Move request from outqueue to empty queue */
125 list_del(&request->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
127 /* Check if there is a pending buffer on the out queue. */
128 request = NULL;
129 if (!list_empty(&sclp_vt220_outqueue))
130 request = list_entry(sclp_vt220_outqueue.next,
131 struct sclp_vt220_request, list);
Michael Holzheu62b749422009-06-16 10:30:40 +0200132 if (!request || sclp_vt220_suspended) {
133 sclp_vt220_queue_running = 0;
134 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
135 break;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200138 } while (__sclp_vt220_emit(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (request == NULL && sclp_vt220_flush_later)
140 sclp_vt220_emit_current();
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100141 tty_port_tty_wakeup(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144#define SCLP_BUFFER_MAX_RETRY 1
145
146/*
147 * Callback through which the result of a write request is reported by the
148 * SCLP.
149 */
150static void
151sclp_vt220_callback(struct sclp_req *request, void *data)
152{
153 struct sclp_vt220_request *vt220_request;
154 struct sclp_vt220_sccb *sccb;
155
156 vt220_request = (struct sclp_vt220_request *) data;
157 if (request->status == SCLP_REQ_FAILED) {
158 sclp_vt220_process_queue(vt220_request);
159 return;
160 }
161 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
162
163 /* Check SCLP response code and choose suitable action */
164 switch (sccb->header.response_code) {
165 case 0x0020 :
166 break;
167
168 case 0x05f0: /* Target resource in improper state */
169 break;
170
171 case 0x0340: /* Contained SCLP equipment check */
172 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
173 break;
174 /* Remove processed buffers and requeue rest */
175 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
176 /* Not all buffers were processed */
177 sccb->header.response_code = 0x0000;
178 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
179 if (sclp_add_request(request) == 0)
180 return;
181 }
182 break;
183
184 case 0x0040: /* SCLP equipment check */
185 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
186 break;
187 sccb->header.response_code = 0x0000;
188 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
189 if (sclp_add_request(request) == 0)
190 return;
191 break;
192
193 default:
194 break;
195 }
196 sclp_vt220_process_queue(vt220_request);
197}
198
199/*
200 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
201 * otherwise.
202 */
203static int
204__sclp_vt220_emit(struct sclp_vt220_request *request)
205{
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100206 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 request->sclp_req.status = SCLP_REQ_FAILED;
208 return -EIO;
209 }
Heiko Carstensab14de62007-02-05 21:18:37 +0100210 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 request->sclp_req.status = SCLP_REQ_FILLED;
212 request->sclp_req.callback = sclp_vt220_callback;
213 request->sclp_req.callback_data = (void *) request;
214
215 return sclp_add_request(&request->sclp_req);
216}
217
218/*
Michael Holzheu62b749422009-06-16 10:30:40 +0200219 * Queue and emit current request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221static void
222sclp_vt220_emit_current(void)
223{
224 unsigned long flags;
225 struct sclp_vt220_request *request;
226 struct sclp_vt220_sccb *sccb;
227
228 spin_lock_irqsave(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200229 if (sclp_vt220_current_request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 sccb = (struct sclp_vt220_sccb *)
231 sclp_vt220_current_request->sclp_req.sccb;
232 /* Only emit buffers with content */
233 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
Michael Holzheu62b749422009-06-16 10:30:40 +0200234 list_add_tail(&sclp_vt220_current_request->list,
235 &sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sclp_vt220_current_request = NULL;
237 if (timer_pending(&sclp_vt220_timer))
238 del_timer(&sclp_vt220_timer);
239 }
240 sclp_vt220_flush_later = 0;
241 }
Michael Holzheu62b749422009-06-16 10:30:40 +0200242 if (sclp_vt220_queue_running || sclp_vt220_suspended)
243 goto out_unlock;
244 if (list_empty(&sclp_vt220_outqueue))
245 goto out_unlock;
246 request = list_first_entry(&sclp_vt220_outqueue,
247 struct sclp_vt220_request, list);
248 sclp_vt220_queue_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200250
251 if (__sclp_vt220_emit(request))
252 sclp_vt220_process_queue(request);
253 return;
254out_unlock:
255 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258#define SCLP_NORMAL_WRITE 0x00
259
260/*
261 * Helper function to initialize a page with the sclp request structure.
262 */
263static struct sclp_vt220_request *
264sclp_vt220_initialize_page(void *page)
265{
266 struct sclp_vt220_request *request;
267 struct sclp_vt220_sccb *sccb;
268
269 /* Place request structure at end of page */
270 request = ((struct sclp_vt220_request *)
271 ((addr_t) page + PAGE_SIZE)) - 1;
272 request->retry_count = 0;
273 request->sclp_req.sccb = page;
274 /* SCCB goes at start of page */
275 sccb = (struct sclp_vt220_sccb *) page;
276 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
277 sccb->header.length = sizeof(struct sclp_vt220_sccb);
278 sccb->header.function_code = SCLP_NORMAL_WRITE;
279 sccb->header.response_code = 0x0000;
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200280 sccb->evbuf.type = EVTYP_VT220MSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sccb->evbuf.length = sizeof(struct evbuf_header);
282
283 return request;
284}
285
286static inline unsigned int
287sclp_vt220_space_left(struct sclp_vt220_request *request)
288{
289 struct sclp_vt220_sccb *sccb;
290 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
291 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
292 sccb->header.length;
293}
294
295static inline unsigned int
296sclp_vt220_chars_stored(struct sclp_vt220_request *request)
297{
298 struct sclp_vt220_sccb *sccb;
299 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
300 return sccb->evbuf.length - sizeof(struct evbuf_header);
301}
302
303/*
304 * Add msg to buffer associated with request. Return the number of characters
305 * added.
306 */
307static int
308sclp_vt220_add_msg(struct sclp_vt220_request *request,
309 const unsigned char *msg, int count, int convertlf)
310{
311 struct sclp_vt220_sccb *sccb;
312 void *buffer;
313 unsigned char c;
314 int from;
315 int to;
316
317 if (count > sclp_vt220_space_left(request))
318 count = sclp_vt220_space_left(request);
319 if (count <= 0)
320 return 0;
321
322 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
323 buffer = (void *) ((addr_t) sccb + sccb->header.length);
324
325 if (convertlf) {
326 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
327 for (from=0, to=0;
328 (from < count) && (to < sclp_vt220_space_left(request));
329 from++) {
330 /* Retrieve character */
331 c = msg[from];
332 /* Perform conversion */
333 if (c == 0x0a) {
334 if (to + 1 < sclp_vt220_space_left(request)) {
335 ((unsigned char *) buffer)[to++] = c;
336 ((unsigned char *) buffer)[to++] = 0x0d;
337 } else
338 break;
339
340 } else
341 ((unsigned char *) buffer)[to++] = c;
342 }
343 sccb->header.length += to;
344 sccb->evbuf.length += to;
345 return from;
346 } else {
347 memcpy(buffer, (const void *) msg, count);
348 sccb->header.length += count;
349 sccb->evbuf.length += count;
350 return count;
351 }
352}
353
354/*
355 * Emit buffer after having waited long enough for more data to arrive.
356 */
357static void
358sclp_vt220_timeout(unsigned long data)
359{
360 sclp_vt220_emit_current();
361}
362
Christian Borntraegerfa331ff2008-03-05 12:37:12 +0100363#define BUFFER_MAX_DELAY HZ/20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/*
366 * Internal implementation of the write function. Write COUNT bytes of data
367 * from memory at BUF
368 * to the SCLP interface. In case that the data does not fit into the current
369 * write buffer, emit the current one and allocate a new one. If there are no
370 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
371 * is non-zero, the buffer will be scheduled for emitting after a timeout -
372 * otherwise the user has to explicitly call the flush function.
373 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
374 * buffer should be converted to 0x0a 0x0d. After completion, return the number
375 * of bytes written.
376 */
377static int
378__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
Heiko Carstensd4820e42008-05-30 10:03:30 +0200379 int convertlf, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 unsigned long flags;
382 void *page;
383 int written;
384 int overall_written;
385
386 if (count <= 0)
387 return 0;
388 overall_written = 0;
389 spin_lock_irqsave(&sclp_vt220_lock, flags);
390 do {
Heiko Carstensd4820e42008-05-30 10:03:30 +0200391 /* Create an sclp output buffer if none exists yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (sclp_vt220_current_request == NULL) {
393 while (list_empty(&sclp_vt220_empty)) {
Heiko Carstensd1e23372008-04-17 07:46:02 +0200394 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200395 if (may_fail || sclp_vt220_suspended)
Heiko Carstensd4820e42008-05-30 10:03:30 +0200396 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 else
Heiko Carstensd4820e42008-05-30 10:03:30 +0200398 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 spin_lock_irqsave(&sclp_vt220_lock, flags);
400 }
401 page = (void *) sclp_vt220_empty.next;
402 list_del((struct list_head *) page);
403 sclp_vt220_current_request =
404 sclp_vt220_initialize_page(page);
405 }
406 /* Try to write the string to the current request buffer */
407 written = sclp_vt220_add_msg(sclp_vt220_current_request,
408 buf, count, convertlf);
409 overall_written += written;
410 if (written == count)
411 break;
412 /*
413 * Not all characters could be written to the current
414 * output buffer. Emit the buffer, create a new buffer
415 * and then output the rest of the string.
416 */
417 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
418 sclp_vt220_emit_current();
419 spin_lock_irqsave(&sclp_vt220_lock, flags);
420 buf += written;
421 count -= written;
422 } while (count > 0);
423 /* Setup timer to output current console buffer after some time */
424 if (sclp_vt220_current_request != NULL &&
425 !timer_pending(&sclp_vt220_timer) && do_schedule) {
426 sclp_vt220_timer.function = sclp_vt220_timeout;
427 sclp_vt220_timer.data = 0UL;
428 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
429 add_timer(&sclp_vt220_timer);
430 }
431 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Heiko Carstensd4820e42008-05-30 10:03:30 +0200432out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return overall_written;
434}
435
436/*
437 * This routine is called by the kernel to write a series of
438 * characters to the tty device. The characters may come from
439 * user space or kernel space. This routine will return the
440 * number of characters actually accepted for writing.
441 */
442static int
443sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
444{
Heiko Carstensd1e23372008-04-17 07:46:02 +0200445 return __sclp_vt220_write(buf, count, 1, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448#define SCLP_VT220_SESSION_ENDED 0x01
449#define SCLP_VT220_SESSION_STARTED 0x80
450#define SCLP_VT220_SESSION_DATA 0x00
451
452/*
453 * Called by the SCLP to report incoming event buffers.
454 */
455static void
456sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
457{
458 char *buffer;
459 unsigned int count;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
462 count = evbuf->length - sizeof(struct evbuf_header);
463
464 switch (*buffer) {
465 case SCLP_VT220_SESSION_ENDED:
466 case SCLP_VT220_SESSION_STARTED:
467 break;
468 case SCLP_VT220_SESSION_DATA:
469 /* Send input to line discipline */
470 buffer++;
471 count--;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100472 tty_insert_flip_string(&sclp_vt220_port, buffer, count);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100473 tty_flip_buffer_push(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 }
476}
477
478/*
479 * This routine is called when a particular tty device is opened.
480 */
481static int
482sclp_vt220_open(struct tty_struct *tty, struct file *filp)
483{
484 if (tty->count == 1) {
Jiri Slaby092f7372012-04-02 13:54:13 +0200485 tty_port_tty_set(&sclp_vt220_port, tty);
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100486 sclp_vt220_port.low_latency = 0;
Hendrik Brueckner0b665d72010-01-27 10:12:38 +0100487 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
488 tty->winsize.ws_row = 24;
489 tty->winsize.ws_col = 80;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 return 0;
493}
494
495/*
496 * This routine is called when a particular tty device is closed.
497 */
498static void
499sclp_vt220_close(struct tty_struct *tty, struct file *filp)
500{
Jiri Slabyb538c4e2012-04-02 13:54:14 +0200501 if (tty->count == 1)
Jiri Slaby092f7372012-04-02 13:54:13 +0200502 tty_port_tty_set(&sclp_vt220_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * This routine is called by the kernel to write a single
507 * character to the tty device. If the kernel uses this routine,
508 * it must call the flush_chars() routine (if defined) when it is
509 * done stuffing characters into the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700511static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
513{
Heiko Carstensd4820e42008-05-30 10:03:30 +0200514 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/*
518 * This routine is called by the kernel after it has written a
519 * series of characters to the tty device using put_char().
520 */
521static void
522sclp_vt220_flush_chars(struct tty_struct *tty)
523{
Michael Holzheu62b749422009-06-16 10:30:40 +0200524 if (!sclp_vt220_queue_running)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sclp_vt220_emit_current();
526 else
527 sclp_vt220_flush_later = 1;
528}
529
530/*
531 * This routine returns the numbers of characters the tty driver
532 * will accept for queuing to be written. This number is subject
533 * to change as output buffers get emptied, or if the output flow
534 * control is acted.
535 */
536static int
537sclp_vt220_write_room(struct tty_struct *tty)
538{
539 unsigned long flags;
540 struct list_head *l;
541 int count;
542
543 spin_lock_irqsave(&sclp_vt220_lock, flags);
544 count = 0;
545 if (sclp_vt220_current_request != NULL)
546 count = sclp_vt220_space_left(sclp_vt220_current_request);
547 list_for_each(l, &sclp_vt220_empty)
548 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
549 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
550 return count;
551}
552
553/*
554 * Return number of buffered chars.
555 */
556static int
557sclp_vt220_chars_in_buffer(struct tty_struct *tty)
558{
559 unsigned long flags;
560 struct list_head *l;
561 struct sclp_vt220_request *r;
562 int count;
563
564 spin_lock_irqsave(&sclp_vt220_lock, flags);
565 count = 0;
566 if (sclp_vt220_current_request != NULL)
567 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
568 list_for_each(l, &sclp_vt220_outqueue) {
569 r = list_entry(l, struct sclp_vt220_request, list);
570 count += sclp_vt220_chars_stored(r);
571 }
572 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
573 return count;
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Pass on all buffers to the hardware. Return only when there are no more
578 * buffers pending.
579 */
580static void
581sclp_vt220_flush_buffer(struct tty_struct *tty)
582{
583 sclp_vt220_emit_current();
584}
585
Peter Oberparleiterad211792008-07-14 09:59:07 +0200586/* Release allocated pages. */
587static void __init __sclp_vt220_free_pages(void)
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200588{
589 struct list_head *page, *p;
590
591 list_for_each_safe(page, p, &sclp_vt220_empty) {
592 list_del(page);
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200593 free_page((unsigned long) page);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200594 }
595}
596
Peter Oberparleiterad211792008-07-14 09:59:07 +0200597/* Release memory and unregister from sclp core. Controlled by init counting -
598 * only the last invoker will actually perform these actions. */
599static void __init __sclp_vt220_cleanup(void)
600{
601 sclp_vt220_init_count--;
602 if (sclp_vt220_init_count != 0)
603 return;
604 sclp_unregister(&sclp_vt220_register);
605 __sclp_vt220_free_pages();
Jiri Slaby191c5f12012-11-15 09:49:56 +0100606 tty_port_destroy(&sclp_vt220_port);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200607}
608
609/* Allocate buffer pages and register with sclp core. Controlled by init
610 * counting - only the first invoker will actually perform these actions. */
611static int __init __sclp_vt220_init(int num_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 void *page;
614 int i;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100615 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Peter Oberparleiterad211792008-07-14 09:59:07 +0200617 sclp_vt220_init_count++;
618 if (sclp_vt220_init_count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 spin_lock_init(&sclp_vt220_lock);
621 INIT_LIST_HEAD(&sclp_vt220_empty);
622 INIT_LIST_HEAD(&sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 init_timer(&sclp_vt220_timer);
Jiri Slaby092f7372012-04-02 13:54:13 +0200624 tty_port_init(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sclp_vt220_current_request = NULL;
626 sclp_vt220_buffered_chars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 sclp_vt220_flush_later = 0;
628
629 /* Allocate pages for output buffering */
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200630 rc = -ENOMEM;
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200631 for (i = 0; i < num_pages; i++) {
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200632 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
633 if (!page)
Peter Oberparleiterad211792008-07-14 09:59:07 +0200634 goto out;
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200635 list_add_tail(page, &sclp_vt220_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100637 rc = sclp_register(&sclp_vt220_register);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200638out:
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100639 if (rc) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200640 __sclp_vt220_free_pages();
641 sclp_vt220_init_count--;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100642 tty_port_destroy(&sclp_vt220_port);
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100643 }
644 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700647static const struct tty_operations sclp_vt220_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 .open = sclp_vt220_open,
649 .close = sclp_vt220_close,
650 .write = sclp_vt220_write,
651 .put_char = sclp_vt220_put_char,
652 .flush_chars = sclp_vt220_flush_chars,
653 .write_room = sclp_vt220_write_room,
654 .chars_in_buffer = sclp_vt220_chars_in_buffer,
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200655 .flush_buffer = sclp_vt220_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656};
657
658/*
659 * Register driver with SCLP and Linux and initialize internal tty structures.
660 */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200661static int __init sclp_vt220_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct tty_driver *driver;
664 int rc;
665
666 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
667 * symmetry between VM and LPAR systems regarding ttyS1. */
668 driver = alloc_tty_driver(1);
669 if (!driver)
670 return -ENOMEM;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200671 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200672 if (rc)
673 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 driver->driver_name = SCLP_VT220_DRIVER_NAME;
676 driver->name = SCLP_VT220_DEVICE_NAME;
677 driver->major = SCLP_VT220_MAJOR;
678 driver->minor_start = SCLP_VT220_MINOR;
679 driver->type = TTY_DRIVER_TYPE_SYSTEM;
680 driver->subtype = SYSTEM_TYPE_TTY;
681 driver->init_termios = tty_std_termios;
682 driver->flags = TTY_DRIVER_REAL_RAW;
683 tty_set_operations(driver, &sclp_vt220_ops);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200684 tty_port_link_device(&sclp_vt220_port, driver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 rc = tty_register_driver(driver);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200687 if (rc)
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100688 goto out_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 sclp_vt220_driver = driver;
690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200692out_init:
Peter Oberparleiterad211792008-07-14 09:59:07 +0200693 __sclp_vt220_cleanup();
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200694out_driver:
695 put_tty_driver(driver);
696 return rc;
697}
698__initcall(sclp_vt220_tty_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100700static void __sclp_vt220_flush_buffer(void)
701{
702 unsigned long flags;
703
704 sclp_vt220_emit_current();
705 spin_lock_irqsave(&sclp_vt220_lock, flags);
706 if (timer_pending(&sclp_vt220_timer))
707 del_timer(&sclp_vt220_timer);
Michael Holzheu62b749422009-06-16 10:30:40 +0200708 while (sclp_vt220_queue_running) {
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100709 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
710 sclp_sync_wait();
711 spin_lock_irqsave(&sclp_vt220_lock, flags);
712 }
713 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
714}
715
Michael Holzheu62b749422009-06-16 10:30:40 +0200716/*
717 * Resume console: If there are cached messages, emit them.
718 */
719static void sclp_vt220_resume(void)
720{
721 unsigned long flags;
722
723 spin_lock_irqsave(&sclp_vt220_lock, flags);
724 sclp_vt220_suspended = 0;
725 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
726 sclp_vt220_emit_current();
727}
728
729/*
730 * Suspend console: Set suspend flag and flush console
731 */
732static void sclp_vt220_suspend(void)
733{
734 unsigned long flags;
735
736 spin_lock_irqsave(&sclp_vt220_lock, flags);
737 sclp_vt220_suspended = 1;
738 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
739 __sclp_vt220_flush_buffer();
740}
741
742static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
743 enum sclp_pm_event sclp_pm_event)
744{
745 switch (sclp_pm_event) {
746 case SCLP_PM_EVENT_FREEZE:
747 sclp_vt220_suspend();
748 break;
749 case SCLP_PM_EVENT_RESTORE:
750 case SCLP_PM_EVENT_THAW:
751 sclp_vt220_resume();
752 break;
753 }
754}
755
Michael Holzheuac522b62009-10-14 12:43:51 +0200756#ifdef CONFIG_SCLP_VT220_CONSOLE
757
758static void
759sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
760{
761 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
762}
763
764static struct tty_driver *
765sclp_vt220_con_device(struct console *c, int *index)
766{
767 *index = 0;
768 return sclp_vt220_driver;
769}
770
Holger Smolinski2332ce12008-10-10 21:33:27 +0200771static int
772sclp_vt220_notify(struct notifier_block *self,
773 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 __sclp_vt220_flush_buffer();
Holger Smolinski2332ce12008-10-10 21:33:27 +0200776 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Holger Smolinski2332ce12008-10-10 21:33:27 +0200779static struct notifier_block on_panic_nb = {
780 .notifier_call = sclp_vt220_notify,
781 .priority = 1,
782};
783
784static struct notifier_block on_reboot_nb = {
785 .notifier_call = sclp_vt220_notify,
786 .priority = 1,
787};
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/* Structure needed to register with printk */
790static struct console sclp_vt220_console =
791{
792 .name = SCLP_VT220_CONSOLE_NAME,
793 .write = sclp_vt220_con_write,
794 .device = sclp_vt220_con_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .flags = CON_PRINTBUFFER,
796 .index = SCLP_VT220_CONSOLE_INDEX
797};
798
799static int __init
800sclp_vt220_con_init(void)
801{
802 int rc;
803
804 if (!CONSOLE_IS_SCLP)
805 return 0;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200806 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (rc)
808 return rc;
809 /* Attach linux console */
Holger Smolinski2332ce12008-10-10 21:33:27 +0200810 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
811 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 register_console(&sclp_vt220_console);
813 return 0;
814}
815
816console_initcall(sclp_vt220_con_init);
817#endif /* CONFIG_SCLP_VT220_CONSOLE */
818