MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / s390 / char / sclp_rw.c
blob296f60d95ef69871d843b8f07573548f93715c01
1 /*
2 * drivers/s390/char/sclp_rw.c
3 * driver: reading from and writing to system console on S/390 via SCLP
5 * S390 version
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
11 #include <linux/config.h>
12 #include <linux/kmod.h>
13 #include <linux/types.h>
14 #include <linux/err.h>
15 #include <linux/string.h>
16 #include <linux/spinlock.h>
17 #include <linux/ctype.h>
18 #include <asm/uaccess.h>
20 #include "sclp.h"
21 #include "sclp_rw.h"
23 #define SCLP_RW_PRINT_HEADER "sclp low level driver: "
26 * The room for the SCCB (only for writing) is not equal to a pages size
27 * (as it is specified as the maximum size in the the SCLP ducumentation)
28 * because of the additional data structure described above.
30 #define MAX_SCCB_ROOM (PAGE_SIZE - sizeof(struct sclp_buffer))
32 /* Event type structure for write message and write priority message */
33 static struct sclp_register sclp_rw_event = {
34 .send_mask = EvTyp_Msg_Mask | EvTyp_PMsgCmd_Mask
38 * Setup a sclp write buffer. Gets a page as input (4K) and returns
39 * a pointer to a struct sclp_buffer structure that is located at the
40 * end of the input page. This reduces the buffer space by a few
41 * bytes but simplifies things.
43 struct sclp_buffer *
44 sclp_make_buffer(void *page, unsigned short columns, unsigned short htab)
46 struct sclp_buffer *buffer;
47 struct write_sccb *sccb;
49 sccb = (struct write_sccb *) page;
51 * We keep the struct sclp_buffer structure at the end
52 * of the sccb page.
54 buffer = ((struct sclp_buffer *) ((addr_t) sccb + PAGE_SIZE)) - 1;
55 buffer->sccb = sccb;
56 buffer->retry_count = 0;
57 init_timer(&buffer->retry_timer);
58 buffer->mto_number = 0;
59 buffer->mto_char_sum = 0;
60 buffer->current_line = NULL;
61 buffer->current_length = 0;
62 buffer->columns = columns;
63 buffer->htab = htab;
65 /* initialize sccb */
66 memset(sccb, 0, sizeof(struct write_sccb));
67 sccb->header.length = sizeof(struct write_sccb);
68 sccb->msg_buf.header.length = sizeof(struct msg_buf);
69 sccb->msg_buf.header.type = EvTyp_Msg;
70 sccb->msg_buf.mdb.header.length = sizeof(struct mdb);
71 sccb->msg_buf.mdb.header.type = 1;
72 sccb->msg_buf.mdb.header.tag = 0xD4C4C240; /* ebcdic "MDB " */
73 sccb->msg_buf.mdb.header.revision_code = 1;
74 sccb->msg_buf.mdb.go.length = sizeof(struct go);
75 sccb->msg_buf.mdb.go.type = 1;
77 return buffer;
81 * Return a pointer to the orignal page that has been used to create
82 * the buffer.
84 void *
85 sclp_unmake_buffer(struct sclp_buffer *buffer)
87 return buffer->sccb;
91 * Initialize a new Message Text Object (MTO) at the end of the provided buffer
92 * with enough room for max_len characters. Return 0 on success.
94 static int
95 sclp_initialize_mto(struct sclp_buffer *buffer, int max_len)
97 struct write_sccb *sccb;
98 struct mto *mto;
99 int mto_size;
101 /* max size of new Message Text Object including message text */
102 mto_size = sizeof(struct mto) + max_len;
104 /* check if current buffer sccb can contain the mto */
105 sccb = buffer->sccb;
106 if ((MAX_SCCB_ROOM - sccb->header.length) < mto_size)
107 return -ENOMEM;
109 /* find address of new message text object */
110 mto = (struct mto *)(((addr_t) sccb) + sccb->header.length);
113 * fill the new Message-Text Object,
114 * starting behind the former last byte of the SCCB
116 memset(mto, 0, sizeof(struct mto));
117 mto->length = sizeof(struct mto);
118 mto->type = 4; /* message text object */
119 mto->line_type_flags = LnTpFlgs_EndText; /* end text */
121 /* set pointer to first byte after struct mto. */
122 buffer->current_line = (char *) (mto + 1);
123 buffer->current_length = 0;
125 return 0;
129 * Finalize MTO initialized by sclp_initialize_mto(), updating the sizes of
130 * MTO, enclosing MDB, event buffer and SCCB.
132 static void
133 sclp_finalize_mto(struct sclp_buffer *buffer)
135 struct write_sccb *sccb;
136 struct mto *mto;
137 int str_len, mto_size;
139 str_len = buffer->current_length;
140 buffer->current_line = NULL;
141 buffer->current_length = 0;
143 /* real size of new Message Text Object including message text */
144 mto_size = sizeof(struct mto) + str_len;
146 /* find address of new message text object */
147 sccb = buffer->sccb;
148 mto = (struct mto *)(((addr_t) sccb) + sccb->header.length);
150 /* set size of message text object */
151 mto->length = mto_size;
154 * update values of sizes
155 * (SCCB, Event(Message) Buffer, Message Data Block)
157 sccb->header.length += mto_size;
158 sccb->msg_buf.header.length += mto_size;
159 sccb->msg_buf.mdb.header.length += mto_size;
162 * count number of buffered messages (= number of Message Text
163 * Objects) and number of buffered characters
164 * for the SCCB currently used for buffering and at all
166 buffer->mto_number++;
167 buffer->mto_char_sum += str_len;
171 * processing of a message including escape characters,
172 * returns number of characters written to the output sccb
173 * ("processed" means that is not guaranteed that the character have already
174 * been sent to the SCLP but that it will be done at least next time the SCLP
175 * is not busy)
178 sclp_write(struct sclp_buffer *buffer, const unsigned char *msg, int count)
180 int spaces, i_msg;
181 int rc;
184 * parse msg for escape sequences (\t,\v ...) and put formated
185 * msg into an mto (created by sclp_initialize_mto).
187 * We have to do this work ourselfs because there is no support for
188 * these characters on the native machine and only partial support
189 * under VM (Why does VM interpret \n but the native machine doesn't ?)
191 * Depending on i/o-control setting the message is always written
192 * immediately or we wait for a final new line maybe coming with the
193 * next message. Besides we avoid a buffer overrun by writing its
194 * content.
196 * RESTRICTIONS:
198 * \r and \b work within one line because we are not able to modify
199 * previous output that have already been accepted by the SCLP.
201 * \t combined with following \r is not correctly represented because
202 * \t is expanded to some spaces but \r does not know about a
203 * previous \t and decreases the current position by one column.
204 * This is in order to a slim and quick implementation.
206 for (i_msg = 0; i_msg < count; i_msg++) {
207 switch (msg[i_msg]) {
208 case '\n': /* new line, line feed (ASCII) */
209 /* check if new mto needs to be created */
210 if (buffer->current_line == NULL) {
211 rc = sclp_initialize_mto(buffer, 0);
212 if (rc)
213 return i_msg;
215 sclp_finalize_mto(buffer);
216 break;
217 case '\a': /* bell, one for several times */
218 /* set SCLP sound alarm bit in General Object */
219 buffer->sccb->msg_buf.mdb.go.general_msg_flags |=
220 GnrlMsgFlgs_SndAlrm;
221 break;
222 case '\t': /* horizontal tabulator */
223 /* check if new mto needs to be created */
224 if (buffer->current_line == NULL) {
225 rc = sclp_initialize_mto(buffer,
226 buffer->columns);
227 if (rc)
228 return i_msg;
230 /* "go to (next htab-boundary + 1, same line)" */
231 do {
232 if (buffer->current_length >= buffer->columns)
233 break;
234 /* ok, add a blank */
235 *buffer->current_line++ = 0x40;
236 buffer->current_length++;
237 } while (buffer->current_length % buffer->htab);
238 break;
239 case '\f': /* form feed */
240 case '\v': /* vertical tabulator */
241 /* "go to (actual column, actual line + 1)" */
242 /* = new line, leading spaces */
243 if (buffer->current_line != NULL) {
244 spaces = buffer->current_length;
245 sclp_finalize_mto(buffer);
246 rc = sclp_initialize_mto(buffer,
247 buffer->columns);
248 if (rc)
249 return i_msg;
250 memset(buffer->current_line, 0x40, spaces);
251 buffer->current_line += spaces;
252 buffer->current_length = spaces;
253 } else {
254 /* one an empty line this is the same as \n */
255 rc = sclp_initialize_mto(buffer,
256 buffer->columns);
257 if (rc)
258 return i_msg;
259 sclp_finalize_mto(buffer);
261 break;
262 case '\b': /* backspace */
263 /* "go to (actual column - 1, actual line)" */
264 /* decrement counter indicating position, */
265 /* do not remove last character */
266 if (buffer->current_line != NULL &&
267 buffer->current_length > 0) {
268 buffer->current_length--;
269 buffer->current_line--;
271 break;
272 case 0x00: /* end of string */
273 /* transfer current line to SCCB */
274 if (buffer->current_line != NULL)
275 sclp_finalize_mto(buffer);
276 /* skip the rest of the message including the 0 byte */
277 i_msg = count - 1;
278 break;
279 default: /* no escape character */
280 /* do not output unprintable characters */
281 if (!isprint(msg[i_msg]))
282 break;
283 /* check if new mto needs to be created */
284 if (buffer->current_line == NULL) {
285 rc = sclp_initialize_mto(buffer,
286 buffer->columns);
287 if (rc)
288 return i_msg;
290 *buffer->current_line++ = sclp_ascebc(msg[i_msg]);
291 buffer->current_length++;
292 break;
294 /* check if current mto is full */
295 if (buffer->current_line != NULL &&
296 buffer->current_length >= buffer->columns)
297 sclp_finalize_mto(buffer);
300 /* return number of processed characters */
301 return i_msg;
305 * Return the number of free bytes in the sccb
308 sclp_buffer_space(struct sclp_buffer *buffer)
310 int count;
312 count = MAX_SCCB_ROOM - buffer->sccb->header.length;
313 if (buffer->current_line != NULL)
314 count -= sizeof(struct mto) + buffer->current_length;
315 return count;
319 * Return number of characters in buffer
322 sclp_chars_in_buffer(struct sclp_buffer *buffer)
324 int count;
326 count = buffer->mto_char_sum;
327 if (buffer->current_line != NULL)
328 count += buffer->current_length;
329 return count;
333 * sets or provides some values that influence the drivers behaviour
335 void
336 sclp_set_columns(struct sclp_buffer *buffer, unsigned short columns)
338 buffer->columns = columns;
339 if (buffer->current_line != NULL &&
340 buffer->current_length > buffer->columns)
341 sclp_finalize_mto(buffer);
344 void
345 sclp_set_htab(struct sclp_buffer *buffer, unsigned short htab)
347 buffer->htab = htab;
351 * called by sclp_console_init and/or sclp_tty_init
354 sclp_rw_init(void)
356 static int init_done = 0;
357 int rc;
359 if (init_done)
360 return 0;
362 rc = sclp_register(&sclp_rw_event);
363 if (rc == 0)
364 init_done = 1;
365 return rc;
368 static void
369 sclp_buffer_retry(unsigned long data)
371 struct sclp_buffer *buffer = (struct sclp_buffer *) data;
372 buffer->request.status = SCLP_REQ_FILLED;
373 buffer->sccb->header.response_code = 0x0000;
374 sclp_add_request(&buffer->request);
377 #define SCLP_BUFFER_MAX_RETRY 5
378 #define SCLP_BUFFER_RETRY_INTERVAL 2
381 * second half of Write Event Data-function that has to be done after
382 * interruption indicating completion of Service Call.
384 static void
385 sclp_writedata_callback(struct sclp_req *request, void *data)
387 int rc;
388 struct sclp_buffer *buffer;
389 struct write_sccb *sccb;
391 buffer = (struct sclp_buffer *) data;
392 sccb = buffer->sccb;
394 if (request->status == SCLP_REQ_FAILED) {
395 if (buffer->callback != NULL)
396 buffer->callback(buffer, -EIO);
397 return;
399 /* check SCLP response code and choose suitable action */
400 switch (sccb->header.response_code) {
401 case 0x0020 :
402 /* Normal completion, buffer processed, message(s) sent */
403 rc = 0;
404 break;
406 case 0x0340: /* Contained SCLP equipment check */
407 if (buffer->retry_count++ > SCLP_BUFFER_MAX_RETRY) {
408 rc = -EIO;
409 break;
411 /* remove processed buffers and requeue rest */
412 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
413 /* not all buffers were processed */
414 sccb->header.response_code = 0x0000;
415 buffer->request.status = SCLP_REQ_FILLED;
416 sclp_add_request(request);
417 return;
419 rc = 0;
420 break;
422 case 0x0040: /* SCLP equipment check */
423 case 0x05f0: /* Target resource in improper state */
424 if (buffer->retry_count++ > SCLP_BUFFER_MAX_RETRY) {
425 rc = -EIO;
426 break;
428 /* wait some time, then retry request */
429 buffer->retry_timer.function = sclp_buffer_retry;
430 buffer->retry_timer.data = (unsigned long) buffer;
431 buffer->retry_timer.expires = jiffies +
432 SCLP_BUFFER_RETRY_INTERVAL*HZ;
433 add_timer(&buffer->retry_timer);
434 return;
436 default:
437 if (sccb->header.response_code == 0x71f0)
438 rc = -ENOMEM;
439 else
440 rc = -EINVAL;
441 break;
443 if (buffer->callback != NULL)
444 buffer->callback(buffer, rc);
448 * Setup the request structure in the struct sclp_buffer to do SCLP Write
449 * Event Data and pass the request to the core SCLP loop.
451 void
452 sclp_emit_buffer(struct sclp_buffer *buffer,
453 void (*callback)(struct sclp_buffer *, int))
455 struct write_sccb *sccb;
457 /* add current line if there is one */
458 if (buffer->current_line != NULL)
459 sclp_finalize_mto(buffer);
461 /* Are there messages in the output buffer ? */
462 if (buffer->mto_number == 0) {
463 if (callback != NULL)
464 callback(buffer, 0);
465 return;
468 sccb = buffer->sccb;
469 if (sclp_rw_event.sclp_send_mask & EvTyp_Msg_Mask)
470 /* Use normal write message */
471 sccb->msg_buf.header.type = EvTyp_Msg;
472 else if (sclp_rw_event.sclp_send_mask & EvTyp_PMsgCmd_Mask)
473 /* Use write priority message */
474 sccb->msg_buf.header.type = EvTyp_PMsgCmd;
475 else {
476 if (callback != NULL)
477 callback(buffer, -ENOSYS);
478 return;
480 buffer->request.command = SCLP_CMDW_WRITEDATA;
481 buffer->request.status = SCLP_REQ_FILLED;
482 buffer->request.callback = sclp_writedata_callback;
483 buffer->request.callback_data = buffer;
484 buffer->request.sccb = sccb;
485 buffer->callback = callback;
486 sclp_add_request(&buffer->request);