sclp_vt220.c 21 KB

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