sclp_vt220.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * drivers/s390/char/sclp_vt220.c
  3. * SCLP VT220 terminal driver.
  4. *
  5. * S390 version
  6. * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
  8. */
  9. #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>
  17. #include <linux/tty_flip.h>
  18. #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>
  23. #include <linux/bootmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <asm/uaccess.h>
  27. #include "sclp.h"
  28. #define SCLP_VT220_PRINT_HEADER "sclp vt220 tty driver: "
  29. #define SCLP_VT220_MAJOR TTY_MAJOR
  30. #define SCLP_VT220_MINOR 65
  31. #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
  32. #define SCLP_VT220_DEVICE_NAME "ttysclp"
  33. #define SCLP_VT220_CONSOLE_NAME "ttyS"
  34. #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
  35. #define SCLP_VT220_BUF_SIZE 80
  36. /* Representation of a single write request */
  37. struct sclp_vt220_request {
  38. struct list_head list;
  39. struct sclp_req sclp_req;
  40. int retry_count;
  41. };
  42. /* VT220 SCCB */
  43. struct sclp_vt220_sccb {
  44. struct sccb_header header;
  45. struct evbuf_header evbuf;
  46. };
  47. #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
  48. sizeof(struct sclp_vt220_request) - \
  49. sizeof(struct sclp_vt220_sccb))
  50. /* Structures and data needed to register tty driver */
  51. static struct tty_driver *sclp_vt220_driver;
  52. /* The tty_struct that the kernel associated with us */
  53. static struct tty_struct *sclp_vt220_tty;
  54. /* Lock to protect internal data from concurrent access */
  55. static spinlock_t sclp_vt220_lock;
  56. /* List of empty pages to be used as write request buffers */
  57. static struct list_head sclp_vt220_empty;
  58. /* List of pending requests */
  59. static struct list_head sclp_vt220_outqueue;
  60. /* Number of requests in outqueue */
  61. static int sclp_vt220_outqueue_count;
  62. /* Wait queue used to delay write requests while we've run out of buffers */
  63. static wait_queue_head_t sclp_vt220_waitq;
  64. /* Timer used for delaying write requests to merge subsequent messages into
  65. * a single buffer */
  66. static struct timer_list sclp_vt220_timer;
  67. /* Pointer to current request buffer which has been partially filled but not
  68. * yet sent */
  69. static struct sclp_vt220_request *sclp_vt220_current_request;
  70. /* Number of characters in current request buffer */
  71. static int sclp_vt220_buffered_chars;
  72. /* Flag indicating whether this driver has already been initialized */
  73. static int sclp_vt220_initialized = 0;
  74. /* Flag indicating that sclp_vt220_current_request should really
  75. * have been already queued but wasn't because the SCLP was processing
  76. * another buffer */
  77. static int sclp_vt220_flush_later;
  78. static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
  79. static int __sclp_vt220_emit(struct sclp_vt220_request *request);
  80. static void sclp_vt220_emit_current(void);
  81. /* Registration structure for our interest in SCLP event buffers */
  82. static struct sclp_register sclp_vt220_register = {
  83. .send_mask = EVTYP_VT220MSG_MASK,
  84. .receive_mask = EVTYP_VT220MSG_MASK,
  85. .state_change_fn = NULL,
  86. .receiver_fn = sclp_vt220_receiver_fn
  87. };
  88. /*
  89. * Put provided request buffer back into queue and check emit pending
  90. * buffers if necessary.
  91. */
  92. static void
  93. sclp_vt220_process_queue(struct sclp_vt220_request *request)
  94. {
  95. unsigned long flags;
  96. void *page;
  97. do {
  98. /* Put buffer back to list of empty buffers */
  99. page = request->sclp_req.sccb;
  100. spin_lock_irqsave(&sclp_vt220_lock, flags);
  101. /* Move request from outqueue to empty queue */
  102. list_del(&request->list);
  103. sclp_vt220_outqueue_count--;
  104. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  105. /* Check if there is a pending buffer on the out queue. */
  106. request = NULL;
  107. if (!list_empty(&sclp_vt220_outqueue))
  108. request = list_entry(sclp_vt220_outqueue.next,
  109. struct sclp_vt220_request, list);
  110. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  111. } while (request && __sclp_vt220_emit(request));
  112. if (request == NULL && sclp_vt220_flush_later)
  113. sclp_vt220_emit_current();
  114. wake_up(&sclp_vt220_waitq);
  115. /* Check if the tty needs a wake up call */
  116. if (sclp_vt220_tty != NULL) {
  117. tty_wakeup(sclp_vt220_tty);
  118. }
  119. }
  120. #define SCLP_BUFFER_MAX_RETRY 1
  121. /*
  122. * Callback through which the result of a write request is reported by the
  123. * SCLP.
  124. */
  125. static void
  126. sclp_vt220_callback(struct sclp_req *request, void *data)
  127. {
  128. struct sclp_vt220_request *vt220_request;
  129. struct sclp_vt220_sccb *sccb;
  130. vt220_request = (struct sclp_vt220_request *) data;
  131. if (request->status == SCLP_REQ_FAILED) {
  132. sclp_vt220_process_queue(vt220_request);
  133. return;
  134. }
  135. sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
  136. /* Check SCLP response code and choose suitable action */
  137. switch (sccb->header.response_code) {
  138. case 0x0020 :
  139. break;
  140. case 0x05f0: /* Target resource in improper state */
  141. break;
  142. case 0x0340: /* Contained SCLP equipment check */
  143. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  144. break;
  145. /* Remove processed buffers and requeue rest */
  146. if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
  147. /* Not all buffers were processed */
  148. sccb->header.response_code = 0x0000;
  149. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  150. if (sclp_add_request(request) == 0)
  151. return;
  152. }
  153. break;
  154. case 0x0040: /* SCLP equipment check */
  155. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  156. break;
  157. sccb->header.response_code = 0x0000;
  158. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  159. if (sclp_add_request(request) == 0)
  160. return;
  161. break;
  162. default:
  163. break;
  164. }
  165. sclp_vt220_process_queue(vt220_request);
  166. }
  167. /*
  168. * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
  169. * otherwise.
  170. */
  171. static int
  172. __sclp_vt220_emit(struct sclp_vt220_request *request)
  173. {
  174. if (!(sclp_vt220_register.sclp_send_mask & EVTYP_VT220MSG_MASK)) {
  175. request->sclp_req.status = SCLP_REQ_FAILED;
  176. return -EIO;
  177. }
  178. request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
  179. request->sclp_req.status = SCLP_REQ_FILLED;
  180. request->sclp_req.callback = sclp_vt220_callback;
  181. request->sclp_req.callback_data = (void *) request;
  182. return sclp_add_request(&request->sclp_req);
  183. }
  184. /*
  185. * Queue and emit given request.
  186. */
  187. static void
  188. sclp_vt220_emit(struct sclp_vt220_request *request)
  189. {
  190. unsigned long flags;
  191. int count;
  192. spin_lock_irqsave(&sclp_vt220_lock, flags);
  193. list_add_tail(&request->list, &sclp_vt220_outqueue);
  194. count = sclp_vt220_outqueue_count++;
  195. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  196. /* Emit only the first buffer immediately - callback takes care of
  197. * the rest */
  198. if (count == 0 && __sclp_vt220_emit(request))
  199. sclp_vt220_process_queue(request);
  200. }
  201. /*
  202. * Queue and emit current request. Return zero on success, non-zero otherwise.
  203. */
  204. static void
  205. sclp_vt220_emit_current(void)
  206. {
  207. unsigned long flags;
  208. struct sclp_vt220_request *request;
  209. struct sclp_vt220_sccb *sccb;
  210. spin_lock_irqsave(&sclp_vt220_lock, flags);
  211. request = NULL;
  212. if (sclp_vt220_current_request != NULL) {
  213. sccb = (struct sclp_vt220_sccb *)
  214. sclp_vt220_current_request->sclp_req.sccb;
  215. /* Only emit buffers with content */
  216. if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
  217. request = sclp_vt220_current_request;
  218. sclp_vt220_current_request = NULL;
  219. if (timer_pending(&sclp_vt220_timer))
  220. del_timer(&sclp_vt220_timer);
  221. }
  222. sclp_vt220_flush_later = 0;
  223. }
  224. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  225. if (request != NULL)
  226. sclp_vt220_emit(request);
  227. }
  228. #define SCLP_NORMAL_WRITE 0x00
  229. /*
  230. * Helper function to initialize a page with the sclp request structure.
  231. */
  232. static struct sclp_vt220_request *
  233. sclp_vt220_initialize_page(void *page)
  234. {
  235. struct sclp_vt220_request *request;
  236. struct sclp_vt220_sccb *sccb;
  237. /* Place request structure at end of page */
  238. request = ((struct sclp_vt220_request *)
  239. ((addr_t) page + PAGE_SIZE)) - 1;
  240. request->retry_count = 0;
  241. request->sclp_req.sccb = page;
  242. /* SCCB goes at start of page */
  243. sccb = (struct sclp_vt220_sccb *) page;
  244. memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
  245. sccb->header.length = sizeof(struct sclp_vt220_sccb);
  246. sccb->header.function_code = SCLP_NORMAL_WRITE;
  247. sccb->header.response_code = 0x0000;
  248. sccb->evbuf.type = EVTYP_VT220MSG;
  249. sccb->evbuf.length = sizeof(struct evbuf_header);
  250. return request;
  251. }
  252. static inline unsigned int
  253. sclp_vt220_space_left(struct sclp_vt220_request *request)
  254. {
  255. struct sclp_vt220_sccb *sccb;
  256. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  257. return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
  258. sccb->header.length;
  259. }
  260. static inline unsigned int
  261. sclp_vt220_chars_stored(struct sclp_vt220_request *request)
  262. {
  263. struct sclp_vt220_sccb *sccb;
  264. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  265. return sccb->evbuf.length - sizeof(struct evbuf_header);
  266. }
  267. /*
  268. * Add msg to buffer associated with request. Return the number of characters
  269. * added.
  270. */
  271. static int
  272. sclp_vt220_add_msg(struct sclp_vt220_request *request,
  273. const unsigned char *msg, int count, int convertlf)
  274. {
  275. struct sclp_vt220_sccb *sccb;
  276. void *buffer;
  277. unsigned char c;
  278. int from;
  279. int to;
  280. if (count > sclp_vt220_space_left(request))
  281. count = sclp_vt220_space_left(request);
  282. if (count <= 0)
  283. return 0;
  284. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  285. buffer = (void *) ((addr_t) sccb + sccb->header.length);
  286. if (convertlf) {
  287. /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
  288. for (from=0, to=0;
  289. (from < count) && (to < sclp_vt220_space_left(request));
  290. from++) {
  291. /* Retrieve character */
  292. c = msg[from];
  293. /* Perform conversion */
  294. if (c == 0x0a) {
  295. if (to + 1 < sclp_vt220_space_left(request)) {
  296. ((unsigned char *) buffer)[to++] = c;
  297. ((unsigned char *) buffer)[to++] = 0x0d;
  298. } else
  299. break;
  300. } else
  301. ((unsigned char *) buffer)[to++] = c;
  302. }
  303. sccb->header.length += to;
  304. sccb->evbuf.length += to;
  305. return from;
  306. } else {
  307. memcpy(buffer, (const void *) msg, count);
  308. sccb->header.length += count;
  309. sccb->evbuf.length += count;
  310. return count;
  311. }
  312. }
  313. /*
  314. * Emit buffer after having waited long enough for more data to arrive.
  315. */
  316. static void
  317. sclp_vt220_timeout(unsigned long data)
  318. {
  319. sclp_vt220_emit_current();
  320. }
  321. #define BUFFER_MAX_DELAY HZ/2
  322. /*
  323. * Internal implementation of the write function. Write COUNT bytes of data
  324. * from memory at BUF
  325. * to the SCLP interface. In case that the data does not fit into the current
  326. * write buffer, emit the current one and allocate a new one. If there are no
  327. * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
  328. * is non-zero, the buffer will be scheduled for emitting after a timeout -
  329. * otherwise the user has to explicitly call the flush function.
  330. * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
  331. * buffer should be converted to 0x0a 0x0d. After completion, return the number
  332. * of bytes written.
  333. */
  334. static int
  335. __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
  336. int convertlf)
  337. {
  338. unsigned long flags;
  339. void *page;
  340. int written;
  341. int overall_written;
  342. if (count <= 0)
  343. return 0;
  344. overall_written = 0;
  345. spin_lock_irqsave(&sclp_vt220_lock, flags);
  346. do {
  347. /* Create a sclp output buffer if none exists yet */
  348. if (sclp_vt220_current_request == NULL) {
  349. while (list_empty(&sclp_vt220_empty)) {
  350. spin_unlock_irqrestore(&sclp_vt220_lock,
  351. flags);
  352. if (in_interrupt())
  353. sclp_sync_wait();
  354. else
  355. wait_event(sclp_vt220_waitq,
  356. !list_empty(&sclp_vt220_empty));
  357. spin_lock_irqsave(&sclp_vt220_lock, flags);
  358. }
  359. page = (void *) sclp_vt220_empty.next;
  360. list_del((struct list_head *) page);
  361. sclp_vt220_current_request =
  362. sclp_vt220_initialize_page(page);
  363. }
  364. /* Try to write the string to the current request buffer */
  365. written = sclp_vt220_add_msg(sclp_vt220_current_request,
  366. buf, count, convertlf);
  367. overall_written += written;
  368. if (written == count)
  369. break;
  370. /*
  371. * Not all characters could be written to the current
  372. * output buffer. Emit the buffer, create a new buffer
  373. * and then output the rest of the string.
  374. */
  375. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  376. sclp_vt220_emit_current();
  377. spin_lock_irqsave(&sclp_vt220_lock, flags);
  378. buf += written;
  379. count -= written;
  380. } while (count > 0);
  381. /* Setup timer to output current console buffer after some time */
  382. if (sclp_vt220_current_request != NULL &&
  383. !timer_pending(&sclp_vt220_timer) && do_schedule) {
  384. sclp_vt220_timer.function = sclp_vt220_timeout;
  385. sclp_vt220_timer.data = 0UL;
  386. sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
  387. add_timer(&sclp_vt220_timer);
  388. }
  389. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  390. return overall_written;
  391. }
  392. /*
  393. * This routine is called by the kernel to write a series of
  394. * characters to the tty device. The characters may come from
  395. * user space or kernel space. This routine will return the
  396. * number of characters actually accepted for writing.
  397. */
  398. static int
  399. sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
  400. {
  401. return __sclp_vt220_write(buf, count, 1, 0);
  402. }
  403. #define SCLP_VT220_SESSION_ENDED 0x01
  404. #define SCLP_VT220_SESSION_STARTED 0x80
  405. #define SCLP_VT220_SESSION_DATA 0x00
  406. /*
  407. * Called by the SCLP to report incoming event buffers.
  408. */
  409. static void
  410. sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
  411. {
  412. char *buffer;
  413. unsigned int count;
  414. /* Ignore input if device is not open */
  415. if (sclp_vt220_tty == NULL)
  416. return;
  417. buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
  418. count = evbuf->length - sizeof(struct evbuf_header);
  419. switch (*buffer) {
  420. case SCLP_VT220_SESSION_ENDED:
  421. case SCLP_VT220_SESSION_STARTED:
  422. break;
  423. case SCLP_VT220_SESSION_DATA:
  424. /* Send input to line discipline */
  425. buffer++;
  426. count--;
  427. tty_insert_flip_string(sclp_vt220_tty, buffer, count);
  428. tty_flip_buffer_push(sclp_vt220_tty);
  429. break;
  430. }
  431. }
  432. /*
  433. * This routine is called when a particular tty device is opened.
  434. */
  435. static int
  436. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  437. {
  438. if (tty->count == 1) {
  439. sclp_vt220_tty = tty;
  440. tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
  441. if (tty->driver_data == NULL)
  442. return -ENOMEM;
  443. tty->low_latency = 0;
  444. }
  445. return 0;
  446. }
  447. /*
  448. * This routine is called when a particular tty device is closed.
  449. */
  450. static void
  451. sclp_vt220_close(struct tty_struct *tty, struct file *filp)
  452. {
  453. if (tty->count == 1) {
  454. sclp_vt220_tty = NULL;
  455. kfree(tty->driver_data);
  456. tty->driver_data = NULL;
  457. }
  458. }
  459. /*
  460. * This routine is called by the kernel to write a single
  461. * character to the tty device. If the kernel uses this routine,
  462. * it must call the flush_chars() routine (if defined) when it is
  463. * done stuffing characters into the driver.
  464. *
  465. * NOTE: include/linux/tty_driver.h specifies that a character should be
  466. * ignored if there is no room in the queue. This driver implements a different
  467. * semantic in that it will block when there is no more room left.
  468. */
  469. static void
  470. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  471. {
  472. __sclp_vt220_write(&ch, 1, 0, 0);
  473. }
  474. /*
  475. * This routine is called by the kernel after it has written a
  476. * series of characters to the tty device using put_char().
  477. */
  478. static void
  479. sclp_vt220_flush_chars(struct tty_struct *tty)
  480. {
  481. if (sclp_vt220_outqueue_count == 0)
  482. sclp_vt220_emit_current();
  483. else
  484. sclp_vt220_flush_later = 1;
  485. }
  486. /*
  487. * This routine returns the numbers of characters the tty driver
  488. * will accept for queuing to be written. This number is subject
  489. * to change as output buffers get emptied, or if the output flow
  490. * control is acted.
  491. */
  492. static int
  493. sclp_vt220_write_room(struct tty_struct *tty)
  494. {
  495. unsigned long flags;
  496. struct list_head *l;
  497. int count;
  498. spin_lock_irqsave(&sclp_vt220_lock, flags);
  499. count = 0;
  500. if (sclp_vt220_current_request != NULL)
  501. count = sclp_vt220_space_left(sclp_vt220_current_request);
  502. list_for_each(l, &sclp_vt220_empty)
  503. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  504. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  505. return count;
  506. }
  507. /*
  508. * Return number of buffered chars.
  509. */
  510. static int
  511. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  512. {
  513. unsigned long flags;
  514. struct list_head *l;
  515. struct sclp_vt220_request *r;
  516. int count;
  517. spin_lock_irqsave(&sclp_vt220_lock, flags);
  518. count = 0;
  519. if (sclp_vt220_current_request != NULL)
  520. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  521. list_for_each(l, &sclp_vt220_outqueue) {
  522. r = list_entry(l, struct sclp_vt220_request, list);
  523. count += sclp_vt220_chars_stored(r);
  524. }
  525. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  526. return count;
  527. }
  528. static void
  529. __sclp_vt220_flush_buffer(void)
  530. {
  531. unsigned long flags;
  532. sclp_vt220_emit_current();
  533. spin_lock_irqsave(&sclp_vt220_lock, flags);
  534. if (timer_pending(&sclp_vt220_timer))
  535. del_timer(&sclp_vt220_timer);
  536. while (sclp_vt220_outqueue_count > 0) {
  537. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  538. sclp_sync_wait();
  539. spin_lock_irqsave(&sclp_vt220_lock, flags);
  540. }
  541. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  542. }
  543. /*
  544. * Pass on all buffers to the hardware. Return only when there are no more
  545. * buffers pending.
  546. */
  547. static void
  548. sclp_vt220_flush_buffer(struct tty_struct *tty)
  549. {
  550. sclp_vt220_emit_current();
  551. }
  552. /*
  553. * Initialize all relevant components and register driver with system.
  554. */
  555. static int
  556. __sclp_vt220_init(int early)
  557. {
  558. void *page;
  559. int i;
  560. if (sclp_vt220_initialized)
  561. return 0;
  562. sclp_vt220_initialized = 1;
  563. spin_lock_init(&sclp_vt220_lock);
  564. INIT_LIST_HEAD(&sclp_vt220_empty);
  565. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  566. init_waitqueue_head(&sclp_vt220_waitq);
  567. init_timer(&sclp_vt220_timer);
  568. sclp_vt220_current_request = NULL;
  569. sclp_vt220_buffered_chars = 0;
  570. sclp_vt220_outqueue_count = 0;
  571. sclp_vt220_tty = NULL;
  572. sclp_vt220_flush_later = 0;
  573. /* Allocate pages for output buffering */
  574. for (i = 0; i < (early ? MAX_CONSOLE_PAGES : MAX_KMEM_PAGES); i++) {
  575. if (early)
  576. page = alloc_bootmem_low_pages(PAGE_SIZE);
  577. else
  578. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  579. if (!page)
  580. return -ENOMEM;
  581. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  582. }
  583. return 0;
  584. }
  585. static const struct tty_operations sclp_vt220_ops = {
  586. .open = sclp_vt220_open,
  587. .close = sclp_vt220_close,
  588. .write = sclp_vt220_write,
  589. .put_char = sclp_vt220_put_char,
  590. .flush_chars = sclp_vt220_flush_chars,
  591. .write_room = sclp_vt220_write_room,
  592. .chars_in_buffer = sclp_vt220_chars_in_buffer,
  593. .flush_buffer = sclp_vt220_flush_buffer
  594. };
  595. /*
  596. * Register driver with SCLP and Linux and initialize internal tty structures.
  597. */
  598. static int __init
  599. sclp_vt220_tty_init(void)
  600. {
  601. struct tty_driver *driver;
  602. int rc;
  603. /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
  604. * symmetry between VM and LPAR systems regarding ttyS1. */
  605. driver = alloc_tty_driver(1);
  606. if (!driver)
  607. return -ENOMEM;
  608. rc = __sclp_vt220_init(0);
  609. if (rc) {
  610. put_tty_driver(driver);
  611. return rc;
  612. }
  613. rc = sclp_register(&sclp_vt220_register);
  614. if (rc) {
  615. printk(KERN_ERR SCLP_VT220_PRINT_HEADER
  616. "could not register tty - "
  617. "sclp_register returned %d\n", rc);
  618. put_tty_driver(driver);
  619. return rc;
  620. }
  621. driver->owner = THIS_MODULE;
  622. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  623. driver->name = SCLP_VT220_DEVICE_NAME;
  624. driver->major = SCLP_VT220_MAJOR;
  625. driver->minor_start = SCLP_VT220_MINOR;
  626. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  627. driver->subtype = SYSTEM_TYPE_TTY;
  628. driver->init_termios = tty_std_termios;
  629. driver->flags = TTY_DRIVER_REAL_RAW;
  630. tty_set_operations(driver, &sclp_vt220_ops);
  631. rc = tty_register_driver(driver);
  632. if (rc) {
  633. printk(KERN_ERR SCLP_VT220_PRINT_HEADER
  634. "could not register tty - "
  635. "tty_register_driver returned %d\n", rc);
  636. put_tty_driver(driver);
  637. return rc;
  638. }
  639. sclp_vt220_driver = driver;
  640. return 0;
  641. }
  642. module_init(sclp_vt220_tty_init);
  643. #ifdef CONFIG_SCLP_VT220_CONSOLE
  644. static void
  645. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  646. {
  647. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1);
  648. }
  649. static struct tty_driver *
  650. sclp_vt220_con_device(struct console *c, int *index)
  651. {
  652. *index = 0;
  653. return sclp_vt220_driver;
  654. }
  655. /*
  656. * This routine is called from panic when the kernel is going to give up.
  657. * We have to make sure that all buffers will be flushed to the SCLP.
  658. * Note that this function may be called from within an interrupt context.
  659. */
  660. static void
  661. sclp_vt220_con_unblank(void)
  662. {
  663. __sclp_vt220_flush_buffer();
  664. }
  665. /* Structure needed to register with printk */
  666. static struct console sclp_vt220_console =
  667. {
  668. .name = SCLP_VT220_CONSOLE_NAME,
  669. .write = sclp_vt220_con_write,
  670. .device = sclp_vt220_con_device,
  671. .unblank = sclp_vt220_con_unblank,
  672. .flags = CON_PRINTBUFFER,
  673. .index = SCLP_VT220_CONSOLE_INDEX
  674. };
  675. static int __init
  676. sclp_vt220_con_init(void)
  677. {
  678. int rc;
  679. if (!CONSOLE_IS_SCLP)
  680. return 0;
  681. rc = __sclp_vt220_init(1);
  682. if (rc)
  683. return rc;
  684. /* Attach linux console */
  685. register_console(&sclp_vt220_console);
  686. return 0;
  687. }
  688. console_initcall(sclp_vt220_con_init);
  689. #endif /* CONFIG_SCLP_VT220_CONSOLE */