sclp_vt220.c 21 KB

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