sclp_vt220.c 21 KB

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