sclp_vt220.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/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. tty_insert_flip_string(sclp_vt220_tty, buffer, count);
  429. tty_flip_buffer_push(sclp_vt220_tty);
  430. break;
  431. }
  432. }
  433. /*
  434. * This routine is called when a particular tty device is opened.
  435. */
  436. static int
  437. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  438. {
  439. if (tty->count == 1) {
  440. sclp_vt220_tty = tty;
  441. tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
  442. if (tty->driver_data == NULL)
  443. return -ENOMEM;
  444. tty->low_latency = 0;
  445. }
  446. return 0;
  447. }
  448. /*
  449. * This routine is called when a particular tty device is closed.
  450. */
  451. static void
  452. sclp_vt220_close(struct tty_struct *tty, struct file *filp)
  453. {
  454. if (tty->count == 1) {
  455. sclp_vt220_tty = NULL;
  456. kfree(tty->driver_data);
  457. tty->driver_data = NULL;
  458. }
  459. }
  460. /*
  461. * This routine is called by the kernel to write a single
  462. * character to the tty device. If the kernel uses this routine,
  463. * it must call the flush_chars() routine (if defined) when it is
  464. * done stuffing characters into the driver.
  465. *
  466. * NOTE: include/linux/tty_driver.h specifies that a character should be
  467. * ignored if there is no room in the queue. This driver implements a different
  468. * semantic in that it will block when there is no more room left.
  469. */
  470. static void
  471. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  472. {
  473. __sclp_vt220_write(&ch, 1, 0, 0);
  474. }
  475. /*
  476. * This routine is called by the kernel after it has written a
  477. * series of characters to the tty device using put_char().
  478. */
  479. static void
  480. sclp_vt220_flush_chars(struct tty_struct *tty)
  481. {
  482. if (sclp_vt220_outqueue_count == 0)
  483. sclp_vt220_emit_current();
  484. else
  485. sclp_vt220_flush_later = 1;
  486. }
  487. /*
  488. * This routine returns the numbers of characters the tty driver
  489. * will accept for queuing to be written. This number is subject
  490. * to change as output buffers get emptied, or if the output flow
  491. * control is acted.
  492. */
  493. static int
  494. sclp_vt220_write_room(struct tty_struct *tty)
  495. {
  496. unsigned long flags;
  497. struct list_head *l;
  498. int count;
  499. spin_lock_irqsave(&sclp_vt220_lock, flags);
  500. count = 0;
  501. if (sclp_vt220_current_request != NULL)
  502. count = sclp_vt220_space_left(sclp_vt220_current_request);
  503. list_for_each(l, &sclp_vt220_empty)
  504. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  505. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  506. return count;
  507. }
  508. /*
  509. * Return number of buffered chars.
  510. */
  511. static int
  512. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  513. {
  514. unsigned long flags;
  515. struct list_head *l;
  516. struct sclp_vt220_request *r;
  517. int count;
  518. spin_lock_irqsave(&sclp_vt220_lock, flags);
  519. count = 0;
  520. if (sclp_vt220_current_request != NULL)
  521. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  522. list_for_each(l, &sclp_vt220_outqueue) {
  523. r = list_entry(l, struct sclp_vt220_request, list);
  524. count += sclp_vt220_chars_stored(r);
  525. }
  526. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  527. return count;
  528. }
  529. static void
  530. __sclp_vt220_flush_buffer(void)
  531. {
  532. unsigned long flags;
  533. sclp_vt220_emit_current();
  534. spin_lock_irqsave(&sclp_vt220_lock, flags);
  535. if (timer_pending(&sclp_vt220_timer))
  536. del_timer(&sclp_vt220_timer);
  537. while (sclp_vt220_outqueue_count > 0) {
  538. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  539. sclp_sync_wait();
  540. spin_lock_irqsave(&sclp_vt220_lock, flags);
  541. }
  542. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  543. }
  544. /*
  545. * Pass on all buffers to the hardware. Return only when there are no more
  546. * buffers pending.
  547. */
  548. static void
  549. sclp_vt220_flush_buffer(struct tty_struct *tty)
  550. {
  551. sclp_vt220_emit_current();
  552. }
  553. /*
  554. * Initialize all relevant components and register driver with system.
  555. */
  556. static int
  557. __sclp_vt220_init(int early)
  558. {
  559. void *page;
  560. int i;
  561. if (sclp_vt220_initialized)
  562. return 0;
  563. sclp_vt220_initialized = 1;
  564. spin_lock_init(&sclp_vt220_lock);
  565. INIT_LIST_HEAD(&sclp_vt220_empty);
  566. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  567. init_waitqueue_head(&sclp_vt220_waitq);
  568. init_timer(&sclp_vt220_timer);
  569. sclp_vt220_current_request = NULL;
  570. sclp_vt220_buffered_chars = 0;
  571. sclp_vt220_outqueue_count = 0;
  572. sclp_vt220_tty = NULL;
  573. sclp_vt220_flush_later = 0;
  574. /* Allocate pages for output buffering */
  575. for (i = 0; i < (early ? MAX_CONSOLE_PAGES : MAX_KMEM_PAGES); i++) {
  576. if (early)
  577. page = alloc_bootmem_low_pages(PAGE_SIZE);
  578. else
  579. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  580. if (!page)
  581. return -ENOMEM;
  582. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  583. }
  584. return 0;
  585. }
  586. static const struct tty_operations sclp_vt220_ops = {
  587. .open = sclp_vt220_open,
  588. .close = sclp_vt220_close,
  589. .write = sclp_vt220_write,
  590. .put_char = sclp_vt220_put_char,
  591. .flush_chars = sclp_vt220_flush_chars,
  592. .write_room = sclp_vt220_write_room,
  593. .chars_in_buffer = sclp_vt220_chars_in_buffer,
  594. .flush_buffer = sclp_vt220_flush_buffer
  595. };
  596. /*
  597. * Register driver with SCLP and Linux and initialize internal tty structures.
  598. */
  599. int __init
  600. sclp_vt220_tty_init(void)
  601. {
  602. struct tty_driver *driver;
  603. int rc;
  604. /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
  605. * symmetry between VM and LPAR systems regarding ttyS1. */
  606. driver = alloc_tty_driver(1);
  607. if (!driver)
  608. return -ENOMEM;
  609. rc = __sclp_vt220_init(0);
  610. if (rc) {
  611. put_tty_driver(driver);
  612. return rc;
  613. }
  614. rc = sclp_register(&sclp_vt220_register);
  615. if (rc) {
  616. printk(KERN_ERR SCLP_VT220_PRINT_HEADER
  617. "could not register tty - "
  618. "sclp_register returned %d\n", rc);
  619. put_tty_driver(driver);
  620. return rc;
  621. }
  622. driver->owner = THIS_MODULE;
  623. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  624. driver->name = SCLP_VT220_DEVICE_NAME;
  625. driver->major = SCLP_VT220_MAJOR;
  626. driver->minor_start = SCLP_VT220_MINOR;
  627. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  628. driver->subtype = SYSTEM_TYPE_TTY;
  629. driver->init_termios = tty_std_termios;
  630. driver->flags = TTY_DRIVER_REAL_RAW;
  631. tty_set_operations(driver, &sclp_vt220_ops);
  632. rc = tty_register_driver(driver);
  633. if (rc) {
  634. printk(KERN_ERR SCLP_VT220_PRINT_HEADER
  635. "could not register tty - "
  636. "tty_register_driver returned %d\n", rc);
  637. put_tty_driver(driver);
  638. return rc;
  639. }
  640. sclp_vt220_driver = driver;
  641. return 0;
  642. }
  643. module_init(sclp_vt220_tty_init);
  644. #ifdef CONFIG_SCLP_VT220_CONSOLE
  645. static void
  646. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  647. {
  648. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1);
  649. }
  650. static struct tty_driver *
  651. sclp_vt220_con_device(struct console *c, int *index)
  652. {
  653. *index = 0;
  654. return sclp_vt220_driver;
  655. }
  656. /*
  657. * This routine is called from panic when the kernel is going to give up.
  658. * We have to make sure that all buffers will be flushed to the SCLP.
  659. * Note that this function may be called from within an interrupt context.
  660. */
  661. static void
  662. sclp_vt220_con_unblank(void)
  663. {
  664. __sclp_vt220_flush_buffer();
  665. }
  666. /* Structure needed to register with printk */
  667. static struct console sclp_vt220_console =
  668. {
  669. .name = SCLP_VT220_CONSOLE_NAME,
  670. .write = sclp_vt220_con_write,
  671. .device = sclp_vt220_con_device,
  672. .unblank = sclp_vt220_con_unblank,
  673. .flags = CON_PRINTBUFFER,
  674. .index = SCLP_VT220_CONSOLE_INDEX
  675. };
  676. static int __init
  677. sclp_vt220_con_init(void)
  678. {
  679. int rc;
  680. if (!CONSOLE_IS_SCLP)
  681. return 0;
  682. rc = __sclp_vt220_init(1);
  683. if (rc)
  684. return rc;
  685. /* Attach linux console */
  686. register_console(&sclp_vt220_console);
  687. return 0;
  688. }
  689. console_initcall(sclp_vt220_con_init);
  690. #endif /* CONFIG_SCLP_VT220_CONSOLE */