sclp_vt220.c 21 KB

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