blob: 25cf2e8c162aa87c397002a31a1f193178478d0d [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 */
37#define SCLP_VT220_BUF_SIZE 80
38
39/* Representation of a single write request */
40struct sclp_vt220_request {
41 struct list_head list;
42 struct sclp_req sclp_req;
43 int retry_count;
44};
45
46/* VT220 SCCB */
47struct sclp_vt220_sccb {
48 struct sccb_header header;
49 struct evbuf_header evbuf;
50};
51
52#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
53 sizeof(struct sclp_vt220_request) - \
54 sizeof(struct sclp_vt220_sccb))
55
56/* Structures and data needed to register tty driver */
57static struct tty_driver *sclp_vt220_driver;
58
Jiri Slaby092f7372012-04-02 13:54:13 +020059static struct tty_port sclp_vt220_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* Lock to protect internal data from concurrent access */
62static spinlock_t sclp_vt220_lock;
63
64/* List of empty pages to be used as write request buffers */
65static struct list_head sclp_vt220_empty;
66
67/* List of pending requests */
68static struct list_head sclp_vt220_outqueue;
69
Michael Holzheu62b749422009-06-16 10:30:40 +020070/* Suspend mode flag */
71static int sclp_vt220_suspended;
72
73/* Flag that output queue is currently running */
74static int sclp_vt220_queue_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Timer used for delaying write requests to merge subsequent messages into
77 * a single buffer */
78static struct timer_list sclp_vt220_timer;
79
80/* Pointer to current request buffer which has been partially filled but not
81 * yet sent */
82static struct sclp_vt220_request *sclp_vt220_current_request;
83
84/* Number of characters in current request buffer */
85static int sclp_vt220_buffered_chars;
86
Peter Oberparleiterad211792008-07-14 09:59:07 +020087/* Counter controlling core driver initialization. */
88static int __initdata sclp_vt220_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* Flag indicating that sclp_vt220_current_request should really
91 * have been already queued but wasn't because the SCLP was processing
92 * another buffer */
93static int sclp_vt220_flush_later;
94
95static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
Michael Holzheu62b749422009-06-16 10:30:40 +020096static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
97 enum sclp_pm_event sclp_pm_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int __sclp_vt220_emit(struct sclp_vt220_request *request);
99static void sclp_vt220_emit_current(void);
100
101/* Registration structure for our interest in SCLP event buffers */
102static struct sclp_register sclp_vt220_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200103 .send_mask = EVTYP_VT220MSG_MASK,
104 .receive_mask = EVTYP_VT220MSG_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .state_change_fn = NULL,
Michael Holzheu62b749422009-06-16 10:30:40 +0200106 .receiver_fn = sclp_vt220_receiver_fn,
107 .pm_event_fn = sclp_vt220_pm_event_fn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110
111/*
112 * Put provided request buffer back into queue and check emit pending
113 * buffers if necessary.
114 */
115static void
116sclp_vt220_process_queue(struct sclp_vt220_request *request)
117{
Jiri Slaby092f7372012-04-02 13:54:13 +0200118 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned long flags;
120 void *page;
121
122 do {
123 /* Put buffer back to list of empty buffers */
124 page = request->sclp_req.sccb;
125 spin_lock_irqsave(&sclp_vt220_lock, flags);
126 /* Move request from outqueue to empty queue */
127 list_del(&request->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
129 /* Check if there is a pending buffer on the out queue. */
130 request = NULL;
131 if (!list_empty(&sclp_vt220_outqueue))
132 request = list_entry(sclp_vt220_outqueue.next,
133 struct sclp_vt220_request, list);
Michael Holzheu62b749422009-06-16 10:30:40 +0200134 if (!request || sclp_vt220_suspended) {
135 sclp_vt220_queue_running = 0;
136 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
137 break;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200140 } while (__sclp_vt220_emit(request));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (request == NULL && sclp_vt220_flush_later)
142 sclp_vt220_emit_current();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Check if the tty needs a wake up call */
Jiri Slaby092f7372012-04-02 13:54:13 +0200144 tty = tty_port_tty_get(&sclp_vt220_port);
145 if (tty) {
146 tty_wakeup(tty);
147 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149}
150
151#define SCLP_BUFFER_MAX_RETRY 1
152
153/*
154 * Callback through which the result of a write request is reported by the
155 * SCLP.
156 */
157static void
158sclp_vt220_callback(struct sclp_req *request, void *data)
159{
160 struct sclp_vt220_request *vt220_request;
161 struct sclp_vt220_sccb *sccb;
162
163 vt220_request = (struct sclp_vt220_request *) data;
164 if (request->status == SCLP_REQ_FAILED) {
165 sclp_vt220_process_queue(vt220_request);
166 return;
167 }
168 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
169
170 /* Check SCLP response code and choose suitable action */
171 switch (sccb->header.response_code) {
172 case 0x0020 :
173 break;
174
175 case 0x05f0: /* Target resource in improper state */
176 break;
177
178 case 0x0340: /* Contained SCLP equipment check */
179 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
180 break;
181 /* Remove processed buffers and requeue rest */
182 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
183 /* Not all buffers were processed */
184 sccb->header.response_code = 0x0000;
185 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
186 if (sclp_add_request(request) == 0)
187 return;
188 }
189 break;
190
191 case 0x0040: /* SCLP equipment check */
192 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
193 break;
194 sccb->header.response_code = 0x0000;
195 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
196 if (sclp_add_request(request) == 0)
197 return;
198 break;
199
200 default:
201 break;
202 }
203 sclp_vt220_process_queue(vt220_request);
204}
205
206/*
207 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
208 * otherwise.
209 */
210static int
211__sclp_vt220_emit(struct sclp_vt220_request *request)
212{
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100213 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 request->sclp_req.status = SCLP_REQ_FAILED;
215 return -EIO;
216 }
Heiko Carstensab14de62007-02-05 21:18:37 +0100217 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 request->sclp_req.status = SCLP_REQ_FILLED;
219 request->sclp_req.callback = sclp_vt220_callback;
220 request->sclp_req.callback_data = (void *) request;
221
222 return sclp_add_request(&request->sclp_req);
223}
224
225/*
Michael Holzheu62b749422009-06-16 10:30:40 +0200226 * Queue and emit current request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228static void
229sclp_vt220_emit_current(void)
230{
231 unsigned long flags;
232 struct sclp_vt220_request *request;
233 struct sclp_vt220_sccb *sccb;
234
235 spin_lock_irqsave(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200236 if (sclp_vt220_current_request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sccb = (struct sclp_vt220_sccb *)
238 sclp_vt220_current_request->sclp_req.sccb;
239 /* Only emit buffers with content */
240 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
Michael Holzheu62b749422009-06-16 10:30:40 +0200241 list_add_tail(&sclp_vt220_current_request->list,
242 &sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 sclp_vt220_current_request = NULL;
244 if (timer_pending(&sclp_vt220_timer))
245 del_timer(&sclp_vt220_timer);
246 }
247 sclp_vt220_flush_later = 0;
248 }
Michael Holzheu62b749422009-06-16 10:30:40 +0200249 if (sclp_vt220_queue_running || sclp_vt220_suspended)
250 goto out_unlock;
251 if (list_empty(&sclp_vt220_outqueue))
252 goto out_unlock;
253 request = list_first_entry(&sclp_vt220_outqueue,
254 struct sclp_vt220_request, list);
255 sclp_vt220_queue_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200257
258 if (__sclp_vt220_emit(request))
259 sclp_vt220_process_queue(request);
260 return;
261out_unlock:
262 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265#define SCLP_NORMAL_WRITE 0x00
266
267/*
268 * Helper function to initialize a page with the sclp request structure.
269 */
270static struct sclp_vt220_request *
271sclp_vt220_initialize_page(void *page)
272{
273 struct sclp_vt220_request *request;
274 struct sclp_vt220_sccb *sccb;
275
276 /* Place request structure at end of page */
277 request = ((struct sclp_vt220_request *)
278 ((addr_t) page + PAGE_SIZE)) - 1;
279 request->retry_count = 0;
280 request->sclp_req.sccb = page;
281 /* SCCB goes at start of page */
282 sccb = (struct sclp_vt220_sccb *) page;
283 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
284 sccb->header.length = sizeof(struct sclp_vt220_sccb);
285 sccb->header.function_code = SCLP_NORMAL_WRITE;
286 sccb->header.response_code = 0x0000;
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200287 sccb->evbuf.type = EVTYP_VT220MSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 sccb->evbuf.length = sizeof(struct evbuf_header);
289
290 return request;
291}
292
293static inline unsigned int
294sclp_vt220_space_left(struct sclp_vt220_request *request)
295{
296 struct sclp_vt220_sccb *sccb;
297 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
298 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
299 sccb->header.length;
300}
301
302static inline unsigned int
303sclp_vt220_chars_stored(struct sclp_vt220_request *request)
304{
305 struct sclp_vt220_sccb *sccb;
306 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
307 return sccb->evbuf.length - sizeof(struct evbuf_header);
308}
309
310/*
311 * Add msg to buffer associated with request. Return the number of characters
312 * added.
313 */
314static int
315sclp_vt220_add_msg(struct sclp_vt220_request *request,
316 const unsigned char *msg, int count, int convertlf)
317{
318 struct sclp_vt220_sccb *sccb;
319 void *buffer;
320 unsigned char c;
321 int from;
322 int to;
323
324 if (count > sclp_vt220_space_left(request))
325 count = sclp_vt220_space_left(request);
326 if (count <= 0)
327 return 0;
328
329 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
330 buffer = (void *) ((addr_t) sccb + sccb->header.length);
331
332 if (convertlf) {
333 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
334 for (from=0, to=0;
335 (from < count) && (to < sclp_vt220_space_left(request));
336 from++) {
337 /* Retrieve character */
338 c = msg[from];
339 /* Perform conversion */
340 if (c == 0x0a) {
341 if (to + 1 < sclp_vt220_space_left(request)) {
342 ((unsigned char *) buffer)[to++] = c;
343 ((unsigned char *) buffer)[to++] = 0x0d;
344 } else
345 break;
346
347 } else
348 ((unsigned char *) buffer)[to++] = c;
349 }
350 sccb->header.length += to;
351 sccb->evbuf.length += to;
352 return from;
353 } else {
354 memcpy(buffer, (const void *) msg, count);
355 sccb->header.length += count;
356 sccb->evbuf.length += count;
357 return count;
358 }
359}
360
361/*
362 * Emit buffer after having waited long enough for more data to arrive.
363 */
364static void
365sclp_vt220_timeout(unsigned long data)
366{
367 sclp_vt220_emit_current();
368}
369
Christian Borntraegerfa331ff2008-03-05 12:37:12 +0100370#define BUFFER_MAX_DELAY HZ/20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/*
373 * Internal implementation of the write function. Write COUNT bytes of data
374 * from memory at BUF
375 * to the SCLP interface. In case that the data does not fit into the current
376 * write buffer, emit the current one and allocate a new one. If there are no
377 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
378 * is non-zero, the buffer will be scheduled for emitting after a timeout -
379 * otherwise the user has to explicitly call the flush function.
380 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
381 * buffer should be converted to 0x0a 0x0d. After completion, return the number
382 * of bytes written.
383 */
384static int
385__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
Heiko Carstensd4820e42008-05-30 10:03:30 +0200386 int convertlf, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 unsigned long flags;
389 void *page;
390 int written;
391 int overall_written;
392
393 if (count <= 0)
394 return 0;
395 overall_written = 0;
396 spin_lock_irqsave(&sclp_vt220_lock, flags);
397 do {
Heiko Carstensd4820e42008-05-30 10:03:30 +0200398 /* Create an sclp output buffer if none exists yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (sclp_vt220_current_request == NULL) {
400 while (list_empty(&sclp_vt220_empty)) {
Heiko Carstensd1e23372008-04-17 07:46:02 +0200401 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Michael Holzheu62b749422009-06-16 10:30:40 +0200402 if (may_fail || sclp_vt220_suspended)
Heiko Carstensd4820e42008-05-30 10:03:30 +0200403 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 else
Heiko Carstensd4820e42008-05-30 10:03:30 +0200405 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 spin_lock_irqsave(&sclp_vt220_lock, flags);
407 }
408 page = (void *) sclp_vt220_empty.next;
409 list_del((struct list_head *) page);
410 sclp_vt220_current_request =
411 sclp_vt220_initialize_page(page);
412 }
413 /* Try to write the string to the current request buffer */
414 written = sclp_vt220_add_msg(sclp_vt220_current_request,
415 buf, count, convertlf);
416 overall_written += written;
417 if (written == count)
418 break;
419 /*
420 * Not all characters could be written to the current
421 * output buffer. Emit the buffer, create a new buffer
422 * and then output the rest of the string.
423 */
424 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
425 sclp_vt220_emit_current();
426 spin_lock_irqsave(&sclp_vt220_lock, flags);
427 buf += written;
428 count -= written;
429 } while (count > 0);
430 /* Setup timer to output current console buffer after some time */
431 if (sclp_vt220_current_request != NULL &&
432 !timer_pending(&sclp_vt220_timer) && do_schedule) {
433 sclp_vt220_timer.function = sclp_vt220_timeout;
434 sclp_vt220_timer.data = 0UL;
435 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
436 add_timer(&sclp_vt220_timer);
437 }
438 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
Heiko Carstensd4820e42008-05-30 10:03:30 +0200439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return overall_written;
441}
442
443/*
444 * This routine is called by the kernel to write a series of
445 * characters to the tty device. The characters may come from
446 * user space or kernel space. This routine will return the
447 * number of characters actually accepted for writing.
448 */
449static int
450sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
451{
Heiko Carstensd1e23372008-04-17 07:46:02 +0200452 return __sclp_vt220_write(buf, count, 1, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455#define SCLP_VT220_SESSION_ENDED 0x01
456#define SCLP_VT220_SESSION_STARTED 0x80
457#define SCLP_VT220_SESSION_DATA 0x00
458
459/*
460 * Called by the SCLP to report incoming event buffers.
461 */
462static void
463sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
464{
Jiri Slaby092f7372012-04-02 13:54:13 +0200465 struct tty_struct *tty = tty_port_tty_get(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 char *buffer;
467 unsigned int count;
468
469 /* Ignore input if device is not open */
Jiri Slaby092f7372012-04-02 13:54:13 +0200470 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return;
472
473 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
474 count = evbuf->length - sizeof(struct evbuf_header);
475
476 switch (*buffer) {
477 case SCLP_VT220_SESSION_ENDED:
478 case SCLP_VT220_SESSION_STARTED:
479 break;
480 case SCLP_VT220_SESSION_DATA:
481 /* Send input to line discipline */
482 buffer++;
483 count--;
Jiri Slaby092f7372012-04-02 13:54:13 +0200484 tty_insert_flip_string(tty, buffer, count);
485 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 }
Jiri Slaby092f7372012-04-02 13:54:13 +0200488 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/*
492 * This routine is called when a particular tty device is opened.
493 */
494static int
495sclp_vt220_open(struct tty_struct *tty, struct file *filp)
496{
497 if (tty->count == 1) {
Jiri Slaby092f7372012-04-02 13:54:13 +0200498 tty_port_tty_set(&sclp_vt220_port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
500 if (tty->driver_data == NULL)
501 return -ENOMEM;
502 tty->low_latency = 0;
Hendrik Brueckner0b665d72010-01-27 10:12:38 +0100503 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
504 tty->winsize.ws_row = 24;
505 tty->winsize.ws_col = 80;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 return 0;
509}
510
511/*
512 * This routine is called when a particular tty device is closed.
513 */
514static void
515sclp_vt220_close(struct tty_struct *tty, struct file *filp)
516{
517 if (tty->count == 1) {
Jiri Slaby092f7372012-04-02 13:54:13 +0200518 tty_port_tty_set(&sclp_vt220_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 kfree(tty->driver_data);
520 tty->driver_data = NULL;
521 }
522}
523
524/*
525 * This routine is called by the kernel to write a single
526 * character to the tty device. If the kernel uses this routine,
527 * it must call the flush_chars() routine (if defined) when it is
528 * done stuffing characters into the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700530static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
532{
Heiko Carstensd4820e42008-05-30 10:03:30 +0200533 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * This routine is called by the kernel after it has written a
538 * series of characters to the tty device using put_char().
539 */
540static void
541sclp_vt220_flush_chars(struct tty_struct *tty)
542{
Michael Holzheu62b749422009-06-16 10:30:40 +0200543 if (!sclp_vt220_queue_running)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 sclp_vt220_emit_current();
545 else
546 sclp_vt220_flush_later = 1;
547}
548
549/*
550 * This routine returns the numbers of characters the tty driver
551 * will accept for queuing to be written. This number is subject
552 * to change as output buffers get emptied, or if the output flow
553 * control is acted.
554 */
555static int
556sclp_vt220_write_room(struct tty_struct *tty)
557{
558 unsigned long flags;
559 struct list_head *l;
560 int count;
561
562 spin_lock_irqsave(&sclp_vt220_lock, flags);
563 count = 0;
564 if (sclp_vt220_current_request != NULL)
565 count = sclp_vt220_space_left(sclp_vt220_current_request);
566 list_for_each(l, &sclp_vt220_empty)
567 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
568 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
569 return count;
570}
571
572/*
573 * Return number of buffered chars.
574 */
575static int
576sclp_vt220_chars_in_buffer(struct tty_struct *tty)
577{
578 unsigned long flags;
579 struct list_head *l;
580 struct sclp_vt220_request *r;
581 int count;
582
583 spin_lock_irqsave(&sclp_vt220_lock, flags);
584 count = 0;
585 if (sclp_vt220_current_request != NULL)
586 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
587 list_for_each(l, &sclp_vt220_outqueue) {
588 r = list_entry(l, struct sclp_vt220_request, list);
589 count += sclp_vt220_chars_stored(r);
590 }
591 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
592 return count;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * Pass on all buffers to the hardware. Return only when there are no more
597 * buffers pending.
598 */
599static void
600sclp_vt220_flush_buffer(struct tty_struct *tty)
601{
602 sclp_vt220_emit_current();
603}
604
Peter Oberparleiterad211792008-07-14 09:59:07 +0200605/* Release allocated pages. */
606static void __init __sclp_vt220_free_pages(void)
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200607{
608 struct list_head *page, *p;
609
610 list_for_each_safe(page, p, &sclp_vt220_empty) {
611 list_del(page);
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200612 free_page((unsigned long) page);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200613 }
614}
615
Peter Oberparleiterad211792008-07-14 09:59:07 +0200616/* Release memory and unregister from sclp core. Controlled by init counting -
617 * only the last invoker will actually perform these actions. */
618static void __init __sclp_vt220_cleanup(void)
619{
620 sclp_vt220_init_count--;
621 if (sclp_vt220_init_count != 0)
622 return;
623 sclp_unregister(&sclp_vt220_register);
624 __sclp_vt220_free_pages();
625}
626
627/* Allocate buffer pages and register with sclp core. Controlled by init
628 * counting - only the first invoker will actually perform these actions. */
629static int __init __sclp_vt220_init(int num_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 void *page;
632 int i;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100633 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Peter Oberparleiterad211792008-07-14 09:59:07 +0200635 sclp_vt220_init_count++;
636 if (sclp_vt220_init_count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_lock_init(&sclp_vt220_lock);
639 INIT_LIST_HEAD(&sclp_vt220_empty);
640 INIT_LIST_HEAD(&sclp_vt220_outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 init_timer(&sclp_vt220_timer);
Jiri Slaby092f7372012-04-02 13:54:13 +0200642 tty_port_init(&sclp_vt220_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 sclp_vt220_current_request = NULL;
644 sclp_vt220_buffered_chars = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 sclp_vt220_flush_later = 0;
646
647 /* Allocate pages for output buffering */
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200648 rc = -ENOMEM;
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200649 for (i = 0; i < num_pages; i++) {
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200650 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
651 if (!page)
Peter Oberparleiterad211792008-07-14 09:59:07 +0200652 goto out;
Heiko Carstens5c0792f2009-06-22 12:08:07 +0200653 list_add_tail(page, &sclp_vt220_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100655 rc = sclp_register(&sclp_vt220_register);
Peter Oberparleiterad211792008-07-14 09:59:07 +0200656out:
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100657 if (rc) {
Peter Oberparleiterad211792008-07-14 09:59:07 +0200658 __sclp_vt220_free_pages();
659 sclp_vt220_init_count--;
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100660 }
661 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700664static const struct tty_operations sclp_vt220_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .open = sclp_vt220_open,
666 .close = sclp_vt220_close,
667 .write = sclp_vt220_write,
668 .put_char = sclp_vt220_put_char,
669 .flush_chars = sclp_vt220_flush_chars,
670 .write_room = sclp_vt220_write_room,
671 .chars_in_buffer = sclp_vt220_chars_in_buffer,
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200672 .flush_buffer = sclp_vt220_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
674
675/*
676 * Register driver with SCLP and Linux and initialize internal tty structures.
677 */
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200678static int __init sclp_vt220_tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct tty_driver *driver;
681 int rc;
682
683 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
684 * symmetry between VM and LPAR systems regarding ttyS1. */
685 driver = alloc_tty_driver(1);
686 if (!driver)
687 return -ENOMEM;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200688 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200689 if (rc)
690 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 driver->driver_name = SCLP_VT220_DRIVER_NAME;
693 driver->name = SCLP_VT220_DEVICE_NAME;
694 driver->major = SCLP_VT220_MAJOR;
695 driver->minor_start = SCLP_VT220_MINOR;
696 driver->type = TTY_DRIVER_TYPE_SYSTEM;
697 driver->subtype = SYSTEM_TYPE_TTY;
698 driver->init_termios = tty_std_termios;
699 driver->flags = TTY_DRIVER_REAL_RAW;
700 tty_set_operations(driver, &sclp_vt220_ops);
701
702 rc = tty_register_driver(driver);
Martin Schwidefskya12c53f2008-07-14 09:59:28 +0200703 if (rc)
Christian Borntraeger59eb1ca2008-02-09 18:24:33 +0100704 goto out_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sclp_vt220_driver = driver;
706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200708out_init:
Peter Oberparleiterad211792008-07-14 09:59:07 +0200709 __sclp_vt220_cleanup();
Heiko Carstens5aaaf9f2007-07-27 12:29:22 +0200710out_driver:
711 put_tty_driver(driver);
712 return rc;
713}
714__initcall(sclp_vt220_tty_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100716static void __sclp_vt220_flush_buffer(void)
717{
718 unsigned long flags;
719
720 sclp_vt220_emit_current();
721 spin_lock_irqsave(&sclp_vt220_lock, flags);
722 if (timer_pending(&sclp_vt220_timer))
723 del_timer(&sclp_vt220_timer);
Michael Holzheu62b749422009-06-16 10:30:40 +0200724 while (sclp_vt220_queue_running) {
Heiko Carstensb3b59d32008-12-25 13:39:18 +0100725 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
726 sclp_sync_wait();
727 spin_lock_irqsave(&sclp_vt220_lock, flags);
728 }
729 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
730}
731
Michael Holzheu62b749422009-06-16 10:30:40 +0200732/*
733 * Resume console: If there are cached messages, emit them.
734 */
735static void sclp_vt220_resume(void)
736{
737 unsigned long flags;
738
739 spin_lock_irqsave(&sclp_vt220_lock, flags);
740 sclp_vt220_suspended = 0;
741 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
742 sclp_vt220_emit_current();
743}
744
745/*
746 * Suspend console: Set suspend flag and flush console
747 */
748static void sclp_vt220_suspend(void)
749{
750 unsigned long flags;
751
752 spin_lock_irqsave(&sclp_vt220_lock, flags);
753 sclp_vt220_suspended = 1;
754 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
755 __sclp_vt220_flush_buffer();
756}
757
758static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
759 enum sclp_pm_event sclp_pm_event)
760{
761 switch (sclp_pm_event) {
762 case SCLP_PM_EVENT_FREEZE:
763 sclp_vt220_suspend();
764 break;
765 case SCLP_PM_EVENT_RESTORE:
766 case SCLP_PM_EVENT_THAW:
767 sclp_vt220_resume();
768 break;
769 }
770}
771
Michael Holzheuac522b62009-10-14 12:43:51 +0200772#ifdef CONFIG_SCLP_VT220_CONSOLE
773
774static void
775sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
776{
777 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
778}
779
780static struct tty_driver *
781sclp_vt220_con_device(struct console *c, int *index)
782{
783 *index = 0;
784 return sclp_vt220_driver;
785}
786
Holger Smolinski2332ce12008-10-10 21:33:27 +0200787static int
788sclp_vt220_notify(struct notifier_block *self,
789 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 __sclp_vt220_flush_buffer();
Holger Smolinski2332ce12008-10-10 21:33:27 +0200792 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Holger Smolinski2332ce12008-10-10 21:33:27 +0200795static struct notifier_block on_panic_nb = {
796 .notifier_call = sclp_vt220_notify,
797 .priority = 1,
798};
799
800static struct notifier_block on_reboot_nb = {
801 .notifier_call = sclp_vt220_notify,
802 .priority = 1,
803};
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/* Structure needed to register with printk */
806static struct console sclp_vt220_console =
807{
808 .name = SCLP_VT220_CONSOLE_NAME,
809 .write = sclp_vt220_con_write,
810 .device = sclp_vt220_con_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 .flags = CON_PRINTBUFFER,
812 .index = SCLP_VT220_CONSOLE_INDEX
813};
814
815static int __init
816sclp_vt220_con_init(void)
817{
818 int rc;
819
820 if (!CONSOLE_IS_SCLP)
821 return 0;
Peter Oberparleiterad211792008-07-14 09:59:07 +0200822 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (rc)
824 return rc;
825 /* Attach linux console */
Holger Smolinski2332ce12008-10-10 21:33:27 +0200826 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
827 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 register_console(&sclp_vt220_console);
829 return 0;
830}
831
832console_initcall(sclp_vt220_con_init);
833#endif /* CONFIG_SCLP_VT220_CONSOLE */
834