sclp_vt220.c 22 KB

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