sclp_vt220.c 22 KB

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