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