sclp_vt220.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * SCLP VT220 terminal driver.
  3. *
  4. * Copyright IBM Corp. 2003, 2009
  5. *
  6. * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/list.h>
  11. #include <linux/wait.h>
  12. #include <linux/timer.h>
  13. #include <linux/kernel.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/errno.h>
  18. #include <linux/mm.h>
  19. #include <linux/major.h>
  20. #include <linux/console.h>
  21. #include <linux/kdev_t.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/reboot.h>
  25. #include <linux/slab.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. /* Suspend mode flag */
  60. static int sclp_vt220_suspended;
  61. /* Flag that output queue is currently running */
  62. static int sclp_vt220_queue_running;
  63. /* Timer used for delaying write requests to merge subsequent messages into
  64. * a single buffer */
  65. static struct timer_list sclp_vt220_timer;
  66. /* Pointer to current request buffer which has been partially filled but not
  67. * yet sent */
  68. static struct sclp_vt220_request *sclp_vt220_current_request;
  69. /* Number of characters in current request buffer */
  70. static int sclp_vt220_buffered_chars;
  71. /* Counter controlling core driver initialization. */
  72. static int __initdata sclp_vt220_init_count;
  73. /* Flag indicating that sclp_vt220_current_request should really
  74. * have been already queued but wasn't because the SCLP was processing
  75. * another buffer */
  76. static int sclp_vt220_flush_later;
  77. static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
  78. static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  79. enum sclp_pm_event sclp_pm_event);
  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. .pm_event_fn = sclp_vt220_pm_event_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. 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. if (!request || sclp_vt220_suspended) {
  112. sclp_vt220_queue_running = 0;
  113. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  114. break;
  115. }
  116. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  117. } while (__sclp_vt220_emit(request));
  118. if (request == NULL && sclp_vt220_flush_later)
  119. sclp_vt220_emit_current();
  120. /* Check if the tty needs a wake up call */
  121. if (sclp_vt220_tty != NULL) {
  122. tty_wakeup(sclp_vt220_tty);
  123. }
  124. }
  125. #define SCLP_BUFFER_MAX_RETRY 1
  126. /*
  127. * Callback through which the result of a write request is reported by the
  128. * SCLP.
  129. */
  130. static void
  131. sclp_vt220_callback(struct sclp_req *request, void *data)
  132. {
  133. struct sclp_vt220_request *vt220_request;
  134. struct sclp_vt220_sccb *sccb;
  135. vt220_request = (struct sclp_vt220_request *) data;
  136. if (request->status == SCLP_REQ_FAILED) {
  137. sclp_vt220_process_queue(vt220_request);
  138. return;
  139. }
  140. sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
  141. /* Check SCLP response code and choose suitable action */
  142. switch (sccb->header.response_code) {
  143. case 0x0020 :
  144. break;
  145. case 0x05f0: /* Target resource in improper state */
  146. break;
  147. case 0x0340: /* Contained SCLP equipment check */
  148. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  149. break;
  150. /* Remove processed buffers and requeue rest */
  151. if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
  152. /* Not all buffers were processed */
  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. }
  158. break;
  159. case 0x0040: /* SCLP equipment check */
  160. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  161. break;
  162. sccb->header.response_code = 0x0000;
  163. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  164. if (sclp_add_request(request) == 0)
  165. return;
  166. break;
  167. default:
  168. break;
  169. }
  170. sclp_vt220_process_queue(vt220_request);
  171. }
  172. /*
  173. * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
  174. * otherwise.
  175. */
  176. static int
  177. __sclp_vt220_emit(struct sclp_vt220_request *request)
  178. {
  179. if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
  180. request->sclp_req.status = SCLP_REQ_FAILED;
  181. return -EIO;
  182. }
  183. request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
  184. request->sclp_req.status = SCLP_REQ_FILLED;
  185. request->sclp_req.callback = sclp_vt220_callback;
  186. request->sclp_req.callback_data = (void *) request;
  187. return sclp_add_request(&request->sclp_req);
  188. }
  189. /*
  190. * Queue and emit current request.
  191. */
  192. static void
  193. sclp_vt220_emit_current(void)
  194. {
  195. unsigned long flags;
  196. struct sclp_vt220_request *request;
  197. struct sclp_vt220_sccb *sccb;
  198. spin_lock_irqsave(&sclp_vt220_lock, flags);
  199. if (sclp_vt220_current_request) {
  200. sccb = (struct sclp_vt220_sccb *)
  201. sclp_vt220_current_request->sclp_req.sccb;
  202. /* Only emit buffers with content */
  203. if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
  204. list_add_tail(&sclp_vt220_current_request->list,
  205. &sclp_vt220_outqueue);
  206. sclp_vt220_current_request = NULL;
  207. if (timer_pending(&sclp_vt220_timer))
  208. del_timer(&sclp_vt220_timer);
  209. }
  210. sclp_vt220_flush_later = 0;
  211. }
  212. if (sclp_vt220_queue_running || sclp_vt220_suspended)
  213. goto out_unlock;
  214. if (list_empty(&sclp_vt220_outqueue))
  215. goto out_unlock;
  216. request = list_first_entry(&sclp_vt220_outqueue,
  217. struct sclp_vt220_request, list);
  218. sclp_vt220_queue_running = 1;
  219. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  220. if (__sclp_vt220_emit(request))
  221. sclp_vt220_process_queue(request);
  222. return;
  223. out_unlock:
  224. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  225. }
  226. #define SCLP_NORMAL_WRITE 0x00
  227. /*
  228. * Helper function to initialize a page with the sclp request structure.
  229. */
  230. static struct sclp_vt220_request *
  231. sclp_vt220_initialize_page(void *page)
  232. {
  233. struct sclp_vt220_request *request;
  234. struct sclp_vt220_sccb *sccb;
  235. /* Place request structure at end of page */
  236. request = ((struct sclp_vt220_request *)
  237. ((addr_t) page + PAGE_SIZE)) - 1;
  238. request->retry_count = 0;
  239. request->sclp_req.sccb = page;
  240. /* SCCB goes at start of page */
  241. sccb = (struct sclp_vt220_sccb *) page;
  242. memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
  243. sccb->header.length = sizeof(struct sclp_vt220_sccb);
  244. sccb->header.function_code = SCLP_NORMAL_WRITE;
  245. sccb->header.response_code = 0x0000;
  246. sccb->evbuf.type = EVTYP_VT220MSG;
  247. sccb->evbuf.length = sizeof(struct evbuf_header);
  248. return request;
  249. }
  250. static inline unsigned int
  251. sclp_vt220_space_left(struct sclp_vt220_request *request)
  252. {
  253. struct sclp_vt220_sccb *sccb;
  254. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  255. return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
  256. sccb->header.length;
  257. }
  258. static inline unsigned int
  259. sclp_vt220_chars_stored(struct sclp_vt220_request *request)
  260. {
  261. struct sclp_vt220_sccb *sccb;
  262. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  263. return sccb->evbuf.length - sizeof(struct evbuf_header);
  264. }
  265. /*
  266. * Add msg to buffer associated with request. Return the number of characters
  267. * added.
  268. */
  269. static int
  270. sclp_vt220_add_msg(struct sclp_vt220_request *request,
  271. const unsigned char *msg, int count, int convertlf)
  272. {
  273. struct sclp_vt220_sccb *sccb;
  274. void *buffer;
  275. unsigned char c;
  276. int from;
  277. int to;
  278. if (count > sclp_vt220_space_left(request))
  279. count = sclp_vt220_space_left(request);
  280. if (count <= 0)
  281. return 0;
  282. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  283. buffer = (void *) ((addr_t) sccb + sccb->header.length);
  284. if (convertlf) {
  285. /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
  286. for (from=0, to=0;
  287. (from < count) && (to < sclp_vt220_space_left(request));
  288. from++) {
  289. /* Retrieve character */
  290. c = msg[from];
  291. /* Perform conversion */
  292. if (c == 0x0a) {
  293. if (to + 1 < sclp_vt220_space_left(request)) {
  294. ((unsigned char *) buffer)[to++] = c;
  295. ((unsigned char *) buffer)[to++] = 0x0d;
  296. } else
  297. break;
  298. } else
  299. ((unsigned char *) buffer)[to++] = c;
  300. }
  301. sccb->header.length += to;
  302. sccb->evbuf.length += to;
  303. return from;
  304. } else {
  305. memcpy(buffer, (const void *) msg, count);
  306. sccb->header.length += count;
  307. sccb->evbuf.length += count;
  308. return count;
  309. }
  310. }
  311. /*
  312. * Emit buffer after having waited long enough for more data to arrive.
  313. */
  314. static void
  315. sclp_vt220_timeout(unsigned long data)
  316. {
  317. sclp_vt220_emit_current();
  318. }
  319. #define BUFFER_MAX_DELAY HZ/20
  320. /*
  321. * Internal implementation of the write function. Write COUNT bytes of data
  322. * from memory at BUF
  323. * to the SCLP interface. In case that the data does not fit into the current
  324. * write buffer, emit the current one and allocate a new one. If there are no
  325. * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
  326. * is non-zero, the buffer will be scheduled for emitting after a timeout -
  327. * otherwise the user has to explicitly call the flush function.
  328. * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
  329. * buffer should be converted to 0x0a 0x0d. After completion, return the number
  330. * of bytes written.
  331. */
  332. static int
  333. __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
  334. int convertlf, int may_fail)
  335. {
  336. unsigned long flags;
  337. void *page;
  338. int written;
  339. int overall_written;
  340. if (count <= 0)
  341. return 0;
  342. overall_written = 0;
  343. spin_lock_irqsave(&sclp_vt220_lock, flags);
  344. do {
  345. /* Create an sclp output buffer if none exists yet */
  346. if (sclp_vt220_current_request == NULL) {
  347. while (list_empty(&sclp_vt220_empty)) {
  348. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  349. if (may_fail || sclp_vt220_suspended)
  350. goto out;
  351. else
  352. sclp_sync_wait();
  353. spin_lock_irqsave(&sclp_vt220_lock, flags);
  354. }
  355. page = (void *) sclp_vt220_empty.next;
  356. list_del((struct list_head *) page);
  357. sclp_vt220_current_request =
  358. sclp_vt220_initialize_page(page);
  359. }
  360. /* Try to write the string to the current request buffer */
  361. written = sclp_vt220_add_msg(sclp_vt220_current_request,
  362. buf, count, convertlf);
  363. overall_written += written;
  364. if (written == count)
  365. break;
  366. /*
  367. * Not all characters could be written to the current
  368. * output buffer. Emit the buffer, create a new buffer
  369. * and then output the rest of the string.
  370. */
  371. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  372. sclp_vt220_emit_current();
  373. spin_lock_irqsave(&sclp_vt220_lock, flags);
  374. buf += written;
  375. count -= written;
  376. } while (count > 0);
  377. /* Setup timer to output current console buffer after some time */
  378. if (sclp_vt220_current_request != NULL &&
  379. !timer_pending(&sclp_vt220_timer) && do_schedule) {
  380. sclp_vt220_timer.function = sclp_vt220_timeout;
  381. sclp_vt220_timer.data = 0UL;
  382. sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
  383. add_timer(&sclp_vt220_timer);
  384. }
  385. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  386. out:
  387. return overall_written;
  388. }
  389. /*
  390. * This routine is called by the kernel to write a series of
  391. * characters to the tty device. The characters may come from
  392. * user space or kernel space. This routine will return the
  393. * number of characters actually accepted for writing.
  394. */
  395. static int
  396. sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
  397. {
  398. return __sclp_vt220_write(buf, count, 1, 0, 1);
  399. }
  400. #define SCLP_VT220_SESSION_ENDED 0x01
  401. #define SCLP_VT220_SESSION_STARTED 0x80
  402. #define SCLP_VT220_SESSION_DATA 0x00
  403. /*
  404. * Called by the SCLP to report incoming event buffers.
  405. */
  406. static void
  407. sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
  408. {
  409. char *buffer;
  410. unsigned int count;
  411. /* Ignore input if device is not open */
  412. if (sclp_vt220_tty == NULL)
  413. return;
  414. buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
  415. count = evbuf->length - sizeof(struct evbuf_header);
  416. switch (*buffer) {
  417. case SCLP_VT220_SESSION_ENDED:
  418. case SCLP_VT220_SESSION_STARTED:
  419. break;
  420. case SCLP_VT220_SESSION_DATA:
  421. /* Send input to line discipline */
  422. buffer++;
  423. count--;
  424. tty_insert_flip_string(sclp_vt220_tty, buffer, count);
  425. tty_flip_buffer_push(sclp_vt220_tty);
  426. break;
  427. }
  428. }
  429. /*
  430. * This routine is called when a particular tty device is opened.
  431. */
  432. static int
  433. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  434. {
  435. if (tty->count == 1) {
  436. sclp_vt220_tty = tty;
  437. tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
  438. if (tty->driver_data == NULL)
  439. return -ENOMEM;
  440. tty->low_latency = 0;
  441. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  442. tty->winsize.ws_row = 24;
  443. tty->winsize.ws_col = 80;
  444. }
  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. static int
  467. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  468. {
  469. return __sclp_vt220_write(&ch, 1, 0, 0, 1);
  470. }
  471. /*
  472. * This routine is called by the kernel after it has written a
  473. * series of characters to the tty device using put_char().
  474. */
  475. static void
  476. sclp_vt220_flush_chars(struct tty_struct *tty)
  477. {
  478. if (!sclp_vt220_queue_running)
  479. sclp_vt220_emit_current();
  480. else
  481. sclp_vt220_flush_later = 1;
  482. }
  483. /*
  484. * This routine returns the numbers of characters the tty driver
  485. * will accept for queuing to be written. This number is subject
  486. * to change as output buffers get emptied, or if the output flow
  487. * control is acted.
  488. */
  489. static int
  490. sclp_vt220_write_room(struct tty_struct *tty)
  491. {
  492. unsigned long flags;
  493. struct list_head *l;
  494. int count;
  495. spin_lock_irqsave(&sclp_vt220_lock, flags);
  496. count = 0;
  497. if (sclp_vt220_current_request != NULL)
  498. count = sclp_vt220_space_left(sclp_vt220_current_request);
  499. list_for_each(l, &sclp_vt220_empty)
  500. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  501. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  502. return count;
  503. }
  504. /*
  505. * Return number of buffered chars.
  506. */
  507. static int
  508. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  509. {
  510. unsigned long flags;
  511. struct list_head *l;
  512. struct sclp_vt220_request *r;
  513. int count;
  514. spin_lock_irqsave(&sclp_vt220_lock, flags);
  515. count = 0;
  516. if (sclp_vt220_current_request != NULL)
  517. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  518. list_for_each(l, &sclp_vt220_outqueue) {
  519. r = list_entry(l, struct sclp_vt220_request, list);
  520. count += sclp_vt220_chars_stored(r);
  521. }
  522. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  523. return count;
  524. }
  525. /*
  526. * Pass on all buffers to the hardware. Return only when there are no more
  527. * buffers pending.
  528. */
  529. static void
  530. sclp_vt220_flush_buffer(struct tty_struct *tty)
  531. {
  532. sclp_vt220_emit_current();
  533. }
  534. /* Release allocated pages. */
  535. static void __init __sclp_vt220_free_pages(void)
  536. {
  537. struct list_head *page, *p;
  538. list_for_each_safe(page, p, &sclp_vt220_empty) {
  539. list_del(page);
  540. free_page((unsigned long) page);
  541. }
  542. }
  543. /* Release memory and unregister from sclp core. Controlled by init counting -
  544. * only the last invoker will actually perform these actions. */
  545. static void __init __sclp_vt220_cleanup(void)
  546. {
  547. sclp_vt220_init_count--;
  548. if (sclp_vt220_init_count != 0)
  549. return;
  550. sclp_unregister(&sclp_vt220_register);
  551. __sclp_vt220_free_pages();
  552. }
  553. /* Allocate buffer pages and register with sclp core. Controlled by init
  554. * counting - only the first invoker will actually perform these actions. */
  555. static int __init __sclp_vt220_init(int num_pages)
  556. {
  557. void *page;
  558. int i;
  559. int rc;
  560. sclp_vt220_init_count++;
  561. if (sclp_vt220_init_count != 1)
  562. return 0;
  563. spin_lock_init(&sclp_vt220_lock);
  564. INIT_LIST_HEAD(&sclp_vt220_empty);
  565. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  566. init_timer(&sclp_vt220_timer);
  567. sclp_vt220_current_request = NULL;
  568. sclp_vt220_buffered_chars = 0;
  569. sclp_vt220_tty = NULL;
  570. sclp_vt220_flush_later = 0;
  571. /* Allocate pages for output buffering */
  572. rc = -ENOMEM;
  573. for (i = 0; i < num_pages; i++) {
  574. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  575. if (!page)
  576. goto out;
  577. list_add_tail(page, &sclp_vt220_empty);
  578. }
  579. rc = sclp_register(&sclp_vt220_register);
  580. out:
  581. if (rc) {
  582. __sclp_vt220_free_pages();
  583. sclp_vt220_init_count--;
  584. }
  585. return rc;
  586. }
  587. static const 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. static int __init 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(MAX_KMEM_PAGES);
  610. if (rc)
  611. goto out_driver;
  612. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  613. driver->name = SCLP_VT220_DEVICE_NAME;
  614. driver->major = SCLP_VT220_MAJOR;
  615. driver->minor_start = SCLP_VT220_MINOR;
  616. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  617. driver->subtype = SYSTEM_TYPE_TTY;
  618. driver->init_termios = tty_std_termios;
  619. driver->flags = TTY_DRIVER_REAL_RAW;
  620. tty_set_operations(driver, &sclp_vt220_ops);
  621. rc = tty_register_driver(driver);
  622. if (rc)
  623. goto out_init;
  624. sclp_vt220_driver = driver;
  625. return 0;
  626. out_init:
  627. __sclp_vt220_cleanup();
  628. out_driver:
  629. put_tty_driver(driver);
  630. return rc;
  631. }
  632. __initcall(sclp_vt220_tty_init);
  633. static void __sclp_vt220_flush_buffer(void)
  634. {
  635. unsigned long flags;
  636. sclp_vt220_emit_current();
  637. spin_lock_irqsave(&sclp_vt220_lock, flags);
  638. if (timer_pending(&sclp_vt220_timer))
  639. del_timer(&sclp_vt220_timer);
  640. while (sclp_vt220_queue_running) {
  641. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  642. sclp_sync_wait();
  643. spin_lock_irqsave(&sclp_vt220_lock, flags);
  644. }
  645. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  646. }
  647. /*
  648. * Resume console: If there are cached messages, emit them.
  649. */
  650. static void sclp_vt220_resume(void)
  651. {
  652. unsigned long flags;
  653. spin_lock_irqsave(&sclp_vt220_lock, flags);
  654. sclp_vt220_suspended = 0;
  655. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  656. sclp_vt220_emit_current();
  657. }
  658. /*
  659. * Suspend console: Set suspend flag and flush console
  660. */
  661. static void sclp_vt220_suspend(void)
  662. {
  663. unsigned long flags;
  664. spin_lock_irqsave(&sclp_vt220_lock, flags);
  665. sclp_vt220_suspended = 1;
  666. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  667. __sclp_vt220_flush_buffer();
  668. }
  669. static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  670. enum sclp_pm_event sclp_pm_event)
  671. {
  672. switch (sclp_pm_event) {
  673. case SCLP_PM_EVENT_FREEZE:
  674. sclp_vt220_suspend();
  675. break;
  676. case SCLP_PM_EVENT_RESTORE:
  677. case SCLP_PM_EVENT_THAW:
  678. sclp_vt220_resume();
  679. break;
  680. }
  681. }
  682. #ifdef CONFIG_SCLP_VT220_CONSOLE
  683. static void
  684. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  685. {
  686. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
  687. }
  688. static struct tty_driver *
  689. sclp_vt220_con_device(struct console *c, int *index)
  690. {
  691. *index = 0;
  692. return sclp_vt220_driver;
  693. }
  694. static int
  695. sclp_vt220_notify(struct notifier_block *self,
  696. unsigned long event, void *data)
  697. {
  698. __sclp_vt220_flush_buffer();
  699. return NOTIFY_OK;
  700. }
  701. static struct notifier_block on_panic_nb = {
  702. .notifier_call = sclp_vt220_notify,
  703. .priority = 1,
  704. };
  705. static struct notifier_block on_reboot_nb = {
  706. .notifier_call = sclp_vt220_notify,
  707. .priority = 1,
  708. };
  709. /* Structure needed to register with printk */
  710. static struct console sclp_vt220_console =
  711. {
  712. .name = SCLP_VT220_CONSOLE_NAME,
  713. .write = sclp_vt220_con_write,
  714. .device = sclp_vt220_con_device,
  715. .flags = CON_PRINTBUFFER,
  716. .index = SCLP_VT220_CONSOLE_INDEX
  717. };
  718. static int __init
  719. sclp_vt220_con_init(void)
  720. {
  721. int rc;
  722. if (!CONSOLE_IS_SCLP)
  723. return 0;
  724. rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
  725. if (rc)
  726. return rc;
  727. /* Attach linux console */
  728. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  729. register_reboot_notifier(&on_reboot_nb);
  730. register_console(&sclp_vt220_console);
  731. return 0;
  732. }
  733. console_initcall(sclp_vt220_con_init);
  734. #endif /* CONFIG_SCLP_VT220_CONSOLE */