sclp_vt220.c 22 KB

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