con3215.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * 3215 line mode terminal driver.
  4. *
  5. * Copyright IBM Corp. 1999, 2009
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. *
  8. * Updated:
  9. * Aug-2000: Added tab support
  10. * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
  11. */
  12. #include <linux/types.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/vt_kern.h>
  17. #include <linux/init.h>
  18. #include <linux/console.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/err.h>
  21. #include <linux/reboot.h>
  22. #include <linux/serial.h> /* ASYNC_* flags */
  23. #include <linux/slab.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/cio.h>
  26. #include <asm/io.h>
  27. #include <asm/ebcdic.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/delay.h>
  30. #include <asm/cpcmd.h>
  31. #include <asm/setup.h>
  32. #include "ctrlchar.h"
  33. #define NR_3215 1
  34. #define NR_3215_REQ (4*NR_3215)
  35. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  36. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  37. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  38. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  39. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  40. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  41. #define RAW3215_NR_CCWS 3
  42. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  43. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  44. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  45. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  46. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  47. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  48. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  49. #define TAB_STOP_SIZE 8 /* tab stop size */
  50. /*
  51. * Request types for a 3215 device
  52. */
  53. enum raw3215_type {
  54. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  55. };
  56. /*
  57. * Request structure for a 3215 device
  58. */
  59. struct raw3215_req {
  60. enum raw3215_type type; /* type of the request */
  61. int start, len; /* start index & len in output buffer */
  62. int delayable; /* indication to wait for more data */
  63. int residual; /* residual count for read request */
  64. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  65. struct raw3215_info *info; /* pointer to main structure */
  66. struct raw3215_req *next; /* pointer to next request */
  67. } __attribute__ ((aligned(8)));
  68. struct raw3215_info {
  69. struct tty_port port;
  70. struct ccw_device *cdev; /* device for tty driver */
  71. spinlock_t *lock; /* pointer to irq lock */
  72. int flags; /* state flags */
  73. char *buffer; /* pointer to output buffer */
  74. char *inbuf; /* pointer to input buffer */
  75. int head; /* first free byte in output buffer */
  76. int count; /* number of bytes in output buffer */
  77. int written; /* number of bytes in write requests */
  78. struct raw3215_req *queued_read; /* pointer to queued read requests */
  79. struct raw3215_req *queued_write;/* pointer to queued write requests */
  80. struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
  81. wait_queue_head_t empty_wait; /* wait queue for flushing */
  82. struct timer_list timer; /* timer for delayed output */
  83. int line_pos; /* position on the line (for tabs) */
  84. char ubuffer[80]; /* copy_from_user buffer */
  85. };
  86. /* array of 3215 devices structures */
  87. static struct raw3215_info *raw3215[NR_3215];
  88. /* spinlock to protect the raw3215 array */
  89. static DEFINE_SPINLOCK(raw3215_device_lock);
  90. /* list of free request structures */
  91. static struct raw3215_req *raw3215_freelist;
  92. /* spinlock to protect free list */
  93. static spinlock_t raw3215_freelist_lock;
  94. static struct tty_driver *tty3215_driver;
  95. /*
  96. * Get a request structure from the free list
  97. */
  98. static inline struct raw3215_req *raw3215_alloc_req(void)
  99. {
  100. struct raw3215_req *req;
  101. unsigned long flags;
  102. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  103. req = raw3215_freelist;
  104. raw3215_freelist = req->next;
  105. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  106. return req;
  107. }
  108. /*
  109. * Put a request structure back to the free list
  110. */
  111. static inline void raw3215_free_req(struct raw3215_req *req)
  112. {
  113. unsigned long flags;
  114. if (req->type == RAW3215_FREE)
  115. return; /* don't free a free request */
  116. req->type = RAW3215_FREE;
  117. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  118. req->next = raw3215_freelist;
  119. raw3215_freelist = req;
  120. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  121. }
  122. /*
  123. * Set up a read request that reads up to 160 byte from the 3215 device.
  124. * If there is a queued read request it is used, but that shouldn't happen
  125. * because a 3215 terminal won't accept a new read before the old one is
  126. * completed.
  127. */
  128. static void raw3215_mk_read_req(struct raw3215_info *raw)
  129. {
  130. struct raw3215_req *req;
  131. struct ccw1 *ccw;
  132. /* there can only be ONE read request at a time */
  133. req = raw->queued_read;
  134. if (req == NULL) {
  135. /* no queued read request, use new req structure */
  136. req = raw3215_alloc_req();
  137. req->type = RAW3215_READ;
  138. req->info = raw;
  139. raw->queued_read = req;
  140. }
  141. ccw = req->ccws;
  142. ccw->cmd_code = 0x0A; /* read inquiry */
  143. ccw->flags = 0x20; /* ignore incorrect length */
  144. ccw->count = 160;
  145. ccw->cda = (__u32) __pa(raw->inbuf);
  146. }
  147. /*
  148. * Set up a write request with the information from the main structure.
  149. * A ccw chain is created that writes as much as possible from the output
  150. * buffer to the 3215 device. If a queued write exists it is replaced by
  151. * the new, probably lengthened request.
  152. */
  153. static void raw3215_mk_write_req(struct raw3215_info *raw)
  154. {
  155. struct raw3215_req *req;
  156. struct ccw1 *ccw;
  157. int len, count, ix, lines;
  158. if (raw->count <= raw->written)
  159. return;
  160. /* check if there is a queued write request */
  161. req = raw->queued_write;
  162. if (req == NULL) {
  163. /* no queued write request, use new req structure */
  164. req = raw3215_alloc_req();
  165. req->type = RAW3215_WRITE;
  166. req->info = raw;
  167. raw->queued_write = req;
  168. } else {
  169. raw->written -= req->len;
  170. }
  171. ccw = req->ccws;
  172. req->start = (raw->head - raw->count + raw->written) &
  173. (RAW3215_BUFFER_SIZE - 1);
  174. /*
  175. * now we have to count newlines. We can at max accept
  176. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  177. * a restriction in VM
  178. */
  179. lines = 0;
  180. ix = req->start;
  181. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  182. if (raw->buffer[ix] == 0x15)
  183. lines++;
  184. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  185. }
  186. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  187. if (len > RAW3215_MAX_BYTES)
  188. len = RAW3215_MAX_BYTES;
  189. req->len = len;
  190. raw->written += len;
  191. /* set the indication if we should try to enlarge this request */
  192. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  193. ix = req->start;
  194. while (len > 0) {
  195. if (ccw > req->ccws)
  196. ccw[-1].flags |= 0x40; /* use command chaining */
  197. ccw->cmd_code = 0x01; /* write, auto carrier return */
  198. ccw->flags = 0x20; /* ignore incorrect length ind. */
  199. ccw->cda =
  200. (__u32) __pa(raw->buffer + ix);
  201. count = len;
  202. if (ix + count > RAW3215_BUFFER_SIZE)
  203. count = RAW3215_BUFFER_SIZE - ix;
  204. ccw->count = count;
  205. len -= count;
  206. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  207. ccw++;
  208. }
  209. /*
  210. * Add a NOP to the channel program. 3215 devices are purely
  211. * emulated and its much better to avoid the channel end
  212. * interrupt in this case.
  213. */
  214. if (ccw > req->ccws)
  215. ccw[-1].flags |= 0x40; /* use command chaining */
  216. ccw->cmd_code = 0x03; /* NOP */
  217. ccw->flags = 0;
  218. ccw->cda = 0;
  219. ccw->count = 1;
  220. }
  221. /*
  222. * Start a read or a write request
  223. */
  224. static void raw3215_start_io(struct raw3215_info *raw)
  225. {
  226. struct raw3215_req *req;
  227. int res;
  228. req = raw->queued_read;
  229. if (req != NULL &&
  230. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  231. /* dequeue request */
  232. raw->queued_read = NULL;
  233. res = ccw_device_start(raw->cdev, req->ccws,
  234. (unsigned long) req, 0, 0);
  235. if (res != 0) {
  236. /* do_IO failed, put request back to queue */
  237. raw->queued_read = req;
  238. } else {
  239. raw->flags |= RAW3215_WORKING;
  240. }
  241. }
  242. req = raw->queued_write;
  243. if (req != NULL &&
  244. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  245. /* dequeue request */
  246. raw->queued_write = NULL;
  247. res = ccw_device_start(raw->cdev, req->ccws,
  248. (unsigned long) req, 0, 0);
  249. if (res != 0) {
  250. /* do_IO failed, put request back to queue */
  251. raw->queued_write = req;
  252. } else {
  253. raw->flags |= RAW3215_WORKING;
  254. }
  255. }
  256. }
  257. /*
  258. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  259. */
  260. static void raw3215_timeout(unsigned long __data)
  261. {
  262. struct raw3215_info *raw = (struct raw3215_info *) __data;
  263. unsigned long flags;
  264. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  265. raw->flags &= ~RAW3215_TIMER_RUNS;
  266. if (!tty_port_suspended(&raw->port)) {
  267. raw3215_mk_write_req(raw);
  268. raw3215_start_io(raw);
  269. if ((raw->queued_read || raw->queued_write) &&
  270. !(raw->flags & RAW3215_WORKING) &&
  271. !(raw->flags & RAW3215_TIMER_RUNS)) {
  272. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  273. add_timer(&raw->timer);
  274. raw->flags |= RAW3215_TIMER_RUNS;
  275. }
  276. }
  277. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  278. }
  279. /*
  280. * Function to conditionally start an IO. A read is started immediately,
  281. * a write is only started immediately if the flush flag is on or the
  282. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  283. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  284. */
  285. static inline void raw3215_try_io(struct raw3215_info *raw)
  286. {
  287. if (!tty_port_initialized(&raw->port) || tty_port_suspended(&raw->port))
  288. return;
  289. if (raw->queued_read != NULL)
  290. raw3215_start_io(raw);
  291. else if (raw->queued_write != NULL) {
  292. if ((raw->queued_write->delayable == 0) ||
  293. (raw->flags & RAW3215_FLUSHING)) {
  294. /* execute write requests bigger than minimum size */
  295. raw3215_start_io(raw);
  296. }
  297. }
  298. if ((raw->queued_read || raw->queued_write) &&
  299. !(raw->flags & RAW3215_WORKING) &&
  300. !(raw->flags & RAW3215_TIMER_RUNS)) {
  301. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  302. add_timer(&raw->timer);
  303. raw->flags |= RAW3215_TIMER_RUNS;
  304. }
  305. }
  306. /*
  307. * Call tty_wakeup from tasklet context
  308. */
  309. static void raw3215_wakeup(unsigned long data)
  310. {
  311. struct raw3215_info *raw = (struct raw3215_info *) data;
  312. struct tty_struct *tty;
  313. tty = tty_port_tty_get(&raw->port);
  314. if (tty) {
  315. tty_wakeup(tty);
  316. tty_kref_put(tty);
  317. }
  318. }
  319. /*
  320. * Try to start the next IO and wake up processes waiting on the tty.
  321. */
  322. static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
  323. {
  324. raw3215_mk_write_req(raw);
  325. raw3215_try_io(raw);
  326. if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  327. tasklet_schedule(&raw->tlet);
  328. }
  329. /*
  330. * Interrupt routine, called from common io layer
  331. */
  332. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  333. struct irb *irb)
  334. {
  335. struct raw3215_info *raw;
  336. struct raw3215_req *req;
  337. struct tty_struct *tty;
  338. int cstat, dstat;
  339. int count;
  340. raw = dev_get_drvdata(&cdev->dev);
  341. req = (struct raw3215_req *) intparm;
  342. tty = tty_port_tty_get(&raw->port);
  343. cstat = irb->scsw.cmd.cstat;
  344. dstat = irb->scsw.cmd.dstat;
  345. if (cstat != 0)
  346. raw3215_next_io(raw, tty);
  347. if (dstat & 0x01) { /* we got a unit exception */
  348. dstat &= ~0x01; /* we can ignore it */
  349. }
  350. switch (dstat) {
  351. case 0x80:
  352. if (cstat != 0)
  353. break;
  354. /* Attention interrupt, someone hit the enter key */
  355. raw3215_mk_read_req(raw);
  356. raw3215_next_io(raw, tty);
  357. break;
  358. case 0x08:
  359. case 0x0C:
  360. /* Channel end interrupt. */
  361. if ((raw = req->info) == NULL)
  362. goto put_tty; /* That shouldn't happen ... */
  363. if (req->type == RAW3215_READ) {
  364. /* store residual count, then wait for device end */
  365. req->residual = irb->scsw.cmd.count;
  366. }
  367. if (dstat == 0x08)
  368. break;
  369. case 0x04:
  370. /* Device end interrupt. */
  371. if ((raw = req->info) == NULL)
  372. goto put_tty; /* That shouldn't happen ... */
  373. if (req->type == RAW3215_READ && tty != NULL) {
  374. unsigned int cchar;
  375. count = 160 - req->residual;
  376. EBCASC(raw->inbuf, count);
  377. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  378. switch (cchar & CTRLCHAR_MASK) {
  379. case CTRLCHAR_SYSRQ:
  380. break;
  381. case CTRLCHAR_CTRL:
  382. tty_insert_flip_char(&raw->port, cchar,
  383. TTY_NORMAL);
  384. tty_flip_buffer_push(&raw->port);
  385. break;
  386. case CTRLCHAR_NONE:
  387. if (count < 2 ||
  388. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  389. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  390. /* add the auto \n */
  391. raw->inbuf[count] = '\n';
  392. count++;
  393. } else
  394. count -= 2;
  395. tty_insert_flip_string(&raw->port, raw->inbuf,
  396. count);
  397. tty_flip_buffer_push(&raw->port);
  398. break;
  399. }
  400. } else if (req->type == RAW3215_WRITE) {
  401. raw->count -= req->len;
  402. raw->written -= req->len;
  403. }
  404. raw->flags &= ~RAW3215_WORKING;
  405. raw3215_free_req(req);
  406. /* check for empty wait */
  407. if (waitqueue_active(&raw->empty_wait) &&
  408. raw->queued_write == NULL &&
  409. raw->queued_read == NULL) {
  410. wake_up_interruptible(&raw->empty_wait);
  411. }
  412. raw3215_next_io(raw, tty);
  413. break;
  414. default:
  415. /* Strange interrupt, I'll do my best to clean up */
  416. if (req != NULL && req->type != RAW3215_FREE) {
  417. if (req->type == RAW3215_WRITE) {
  418. raw->count -= req->len;
  419. raw->written -= req->len;
  420. }
  421. raw->flags &= ~RAW3215_WORKING;
  422. raw3215_free_req(req);
  423. }
  424. raw3215_next_io(raw, tty);
  425. }
  426. put_tty:
  427. tty_kref_put(tty);
  428. }
  429. /*
  430. * Drop the oldest line from the output buffer.
  431. */
  432. static void raw3215_drop_line(struct raw3215_info *raw)
  433. {
  434. int ix;
  435. char ch;
  436. BUG_ON(raw->written != 0);
  437. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  438. while (raw->count > 0) {
  439. ch = raw->buffer[ix];
  440. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  441. raw->count--;
  442. if (ch == 0x15)
  443. break;
  444. }
  445. raw->head = ix;
  446. }
  447. /*
  448. * Wait until length bytes are available int the output buffer.
  449. * Has to be called with the s390irq lock held. Can be called
  450. * disabled.
  451. */
  452. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  453. {
  454. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  455. /* While console is frozen for suspend we have no other
  456. * choice but to drop message from the buffer to make
  457. * room for even more messages. */
  458. if (tty_port_suspended(&raw->port)) {
  459. raw3215_drop_line(raw);
  460. continue;
  461. }
  462. /* there might be a request pending */
  463. raw->flags |= RAW3215_FLUSHING;
  464. raw3215_mk_write_req(raw);
  465. raw3215_try_io(raw);
  466. raw->flags &= ~RAW3215_FLUSHING;
  467. #ifdef CONFIG_TN3215_CONSOLE
  468. ccw_device_wait_idle(raw->cdev);
  469. #endif
  470. /* Enough room freed up ? */
  471. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  472. break;
  473. /* there might be another cpu waiting for the lock */
  474. spin_unlock(get_ccwdev_lock(raw->cdev));
  475. udelay(100);
  476. spin_lock(get_ccwdev_lock(raw->cdev));
  477. }
  478. }
  479. /*
  480. * String write routine for 3215 devices
  481. */
  482. static void raw3215_write(struct raw3215_info *raw, const char *str,
  483. unsigned int length)
  484. {
  485. unsigned long flags;
  486. int c, count;
  487. while (length > 0) {
  488. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  489. count = (length > RAW3215_BUFFER_SIZE) ?
  490. RAW3215_BUFFER_SIZE : length;
  491. length -= count;
  492. raw3215_make_room(raw, count);
  493. /* copy string to output buffer and convert it to EBCDIC */
  494. while (1) {
  495. c = min_t(int, count,
  496. min(RAW3215_BUFFER_SIZE - raw->count,
  497. RAW3215_BUFFER_SIZE - raw->head));
  498. if (c <= 0)
  499. break;
  500. memcpy(raw->buffer + raw->head, str, c);
  501. ASCEBC(raw->buffer + raw->head, c);
  502. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  503. raw->count += c;
  504. raw->line_pos += c;
  505. str += c;
  506. count -= c;
  507. }
  508. if (!(raw->flags & RAW3215_WORKING)) {
  509. raw3215_mk_write_req(raw);
  510. /* start or queue request */
  511. raw3215_try_io(raw);
  512. }
  513. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  514. }
  515. }
  516. /*
  517. * Put character routine for 3215 devices
  518. */
  519. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  520. {
  521. unsigned long flags;
  522. unsigned int length, i;
  523. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  524. if (ch == '\t') {
  525. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  526. raw->line_pos += length;
  527. ch = ' ';
  528. } else if (ch == '\n') {
  529. length = 1;
  530. raw->line_pos = 0;
  531. } else {
  532. length = 1;
  533. raw->line_pos++;
  534. }
  535. raw3215_make_room(raw, length);
  536. for (i = 0; i < length; i++) {
  537. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  538. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  539. raw->count++;
  540. }
  541. if (!(raw->flags & RAW3215_WORKING)) {
  542. raw3215_mk_write_req(raw);
  543. /* start or queue request */
  544. raw3215_try_io(raw);
  545. }
  546. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  547. }
  548. /*
  549. * Flush routine, it simply sets the flush flag and tries to start
  550. * pending IO.
  551. */
  552. static void raw3215_flush_buffer(struct raw3215_info *raw)
  553. {
  554. unsigned long flags;
  555. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  556. if (raw->count > 0) {
  557. raw->flags |= RAW3215_FLUSHING;
  558. raw3215_try_io(raw);
  559. raw->flags &= ~RAW3215_FLUSHING;
  560. }
  561. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  562. }
  563. /*
  564. * Fire up a 3215 device.
  565. */
  566. static int raw3215_startup(struct raw3215_info *raw)
  567. {
  568. unsigned long flags;
  569. if (tty_port_initialized(&raw->port))
  570. return 0;
  571. raw->line_pos = 0;
  572. tty_port_set_initialized(&raw->port, 1);
  573. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  574. raw3215_try_io(raw);
  575. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  576. return 0;
  577. }
  578. /*
  579. * Shutdown a 3215 device.
  580. */
  581. static void raw3215_shutdown(struct raw3215_info *raw)
  582. {
  583. DECLARE_WAITQUEUE(wait, current);
  584. unsigned long flags;
  585. if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
  586. return;
  587. /* Wait for outstanding requests, then free irq */
  588. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  589. if ((raw->flags & RAW3215_WORKING) ||
  590. raw->queued_write != NULL ||
  591. raw->queued_read != NULL) {
  592. add_wait_queue(&raw->empty_wait, &wait);
  593. set_current_state(TASK_INTERRUPTIBLE);
  594. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  595. schedule();
  596. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  597. remove_wait_queue(&raw->empty_wait, &wait);
  598. set_current_state(TASK_RUNNING);
  599. tty_port_set_initialized(&raw->port, 1);
  600. }
  601. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  602. }
  603. static struct raw3215_info *raw3215_alloc_info(void)
  604. {
  605. struct raw3215_info *info;
  606. info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  607. if (!info)
  608. return NULL;
  609. info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  610. info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  611. if (!info->buffer || !info->inbuf) {
  612. kfree(info->inbuf);
  613. kfree(info->buffer);
  614. kfree(info);
  615. return NULL;
  616. }
  617. setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
  618. init_waitqueue_head(&info->empty_wait);
  619. tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
  620. tty_port_init(&info->port);
  621. return info;
  622. }
  623. static void raw3215_free_info(struct raw3215_info *raw)
  624. {
  625. kfree(raw->inbuf);
  626. kfree(raw->buffer);
  627. tty_port_destroy(&raw->port);
  628. kfree(raw);
  629. }
  630. static int raw3215_probe (struct ccw_device *cdev)
  631. {
  632. struct raw3215_info *raw;
  633. int line;
  634. /* Console is special. */
  635. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  636. return 0;
  637. raw = raw3215_alloc_info();
  638. if (raw == NULL)
  639. return -ENOMEM;
  640. raw->cdev = cdev;
  641. dev_set_drvdata(&cdev->dev, raw);
  642. cdev->handler = raw3215_irq;
  643. spin_lock(&raw3215_device_lock);
  644. for (line = 0; line < NR_3215; line++) {
  645. if (!raw3215[line]) {
  646. raw3215[line] = raw;
  647. break;
  648. }
  649. }
  650. spin_unlock(&raw3215_device_lock);
  651. if (line == NR_3215) {
  652. raw3215_free_info(raw);
  653. return -ENODEV;
  654. }
  655. return 0;
  656. }
  657. static void raw3215_remove (struct ccw_device *cdev)
  658. {
  659. struct raw3215_info *raw;
  660. unsigned int line;
  661. ccw_device_set_offline(cdev);
  662. raw = dev_get_drvdata(&cdev->dev);
  663. if (raw) {
  664. spin_lock(&raw3215_device_lock);
  665. for (line = 0; line < NR_3215; line++)
  666. if (raw3215[line] == raw)
  667. break;
  668. raw3215[line] = NULL;
  669. spin_unlock(&raw3215_device_lock);
  670. dev_set_drvdata(&cdev->dev, NULL);
  671. raw3215_free_info(raw);
  672. }
  673. }
  674. static int raw3215_set_online (struct ccw_device *cdev)
  675. {
  676. struct raw3215_info *raw;
  677. raw = dev_get_drvdata(&cdev->dev);
  678. if (!raw)
  679. return -ENODEV;
  680. return raw3215_startup(raw);
  681. }
  682. static int raw3215_set_offline (struct ccw_device *cdev)
  683. {
  684. struct raw3215_info *raw;
  685. raw = dev_get_drvdata(&cdev->dev);
  686. if (!raw)
  687. return -ENODEV;
  688. raw3215_shutdown(raw);
  689. return 0;
  690. }
  691. static int raw3215_pm_stop(struct ccw_device *cdev)
  692. {
  693. struct raw3215_info *raw;
  694. unsigned long flags;
  695. /* Empty the output buffer, then prevent new I/O. */
  696. raw = dev_get_drvdata(&cdev->dev);
  697. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  698. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  699. tty_port_set_suspended(&raw->port, 1);
  700. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  701. return 0;
  702. }
  703. static int raw3215_pm_start(struct ccw_device *cdev)
  704. {
  705. struct raw3215_info *raw;
  706. unsigned long flags;
  707. /* Allow I/O again and flush output buffer. */
  708. raw = dev_get_drvdata(&cdev->dev);
  709. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  710. tty_port_set_suspended(&raw->port, 0);
  711. raw->flags |= RAW3215_FLUSHING;
  712. raw3215_try_io(raw);
  713. raw->flags &= ~RAW3215_FLUSHING;
  714. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  715. return 0;
  716. }
  717. static struct ccw_device_id raw3215_id[] = {
  718. { CCW_DEVICE(0x3215, 0) },
  719. { /* end of list */ },
  720. };
  721. static struct ccw_driver raw3215_ccw_driver = {
  722. .driver = {
  723. .name = "3215",
  724. .owner = THIS_MODULE,
  725. },
  726. .ids = raw3215_id,
  727. .probe = &raw3215_probe,
  728. .remove = &raw3215_remove,
  729. .set_online = &raw3215_set_online,
  730. .set_offline = &raw3215_set_offline,
  731. .freeze = &raw3215_pm_stop,
  732. .thaw = &raw3215_pm_start,
  733. .restore = &raw3215_pm_start,
  734. .int_class = IRQIO_C15,
  735. };
  736. #ifdef CONFIG_TN3215_CONSOLE
  737. /*
  738. * Write a string to the 3215 console
  739. */
  740. static void con3215_write(struct console *co, const char *str,
  741. unsigned int count)
  742. {
  743. struct raw3215_info *raw;
  744. int i;
  745. if (count <= 0)
  746. return;
  747. raw = raw3215[0]; /* console 3215 is the first one */
  748. while (count > 0) {
  749. for (i = 0; i < count; i++)
  750. if (str[i] == '\t' || str[i] == '\n')
  751. break;
  752. raw3215_write(raw, str, i);
  753. count -= i;
  754. str += i;
  755. if (count > 0) {
  756. raw3215_putchar(raw, *str);
  757. count--;
  758. str++;
  759. }
  760. }
  761. }
  762. static struct tty_driver *con3215_device(struct console *c, int *index)
  763. {
  764. *index = c->index;
  765. return tty3215_driver;
  766. }
  767. /*
  768. * panic() calls con3215_flush through a panic_notifier
  769. * before the system enters a disabled, endless loop.
  770. */
  771. static void con3215_flush(void)
  772. {
  773. struct raw3215_info *raw;
  774. unsigned long flags;
  775. raw = raw3215[0]; /* console 3215 is the first one */
  776. if (tty_port_suspended(&raw->port))
  777. /* The console is still frozen for suspend. */
  778. if (ccw_device_force_console(raw->cdev))
  779. /* Forcing didn't work, no panic message .. */
  780. return;
  781. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  782. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  783. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  784. }
  785. static int con3215_notify(struct notifier_block *self,
  786. unsigned long event, void *data)
  787. {
  788. con3215_flush();
  789. return NOTIFY_OK;
  790. }
  791. static struct notifier_block on_panic_nb = {
  792. .notifier_call = con3215_notify,
  793. .priority = 0,
  794. };
  795. static struct notifier_block on_reboot_nb = {
  796. .notifier_call = con3215_notify,
  797. .priority = 0,
  798. };
  799. /*
  800. * The console structure for the 3215 console
  801. */
  802. static struct console con3215 = {
  803. .name = "ttyS",
  804. .write = con3215_write,
  805. .device = con3215_device,
  806. .flags = CON_PRINTBUFFER,
  807. };
  808. /*
  809. * 3215 console initialization code called from console_init().
  810. */
  811. static int __init con3215_init(void)
  812. {
  813. struct ccw_device *cdev;
  814. struct raw3215_info *raw;
  815. struct raw3215_req *req;
  816. int i;
  817. /* Check if 3215 is to be the console */
  818. if (!CONSOLE_IS_3215)
  819. return -ENODEV;
  820. /* Set the console mode for VM */
  821. if (MACHINE_IS_VM) {
  822. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  823. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  824. }
  825. /* allocate 3215 request structures */
  826. raw3215_freelist = NULL;
  827. spin_lock_init(&raw3215_freelist_lock);
  828. for (i = 0; i < NR_3215_REQ; i++) {
  829. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  830. if (!req)
  831. return -ENOMEM;
  832. req->next = raw3215_freelist;
  833. raw3215_freelist = req;
  834. }
  835. cdev = ccw_device_create_console(&raw3215_ccw_driver);
  836. if (IS_ERR(cdev))
  837. return -ENODEV;
  838. raw3215[0] = raw = raw3215_alloc_info();
  839. raw->cdev = cdev;
  840. dev_set_drvdata(&cdev->dev, raw);
  841. cdev->handler = raw3215_irq;
  842. raw->flags |= RAW3215_FIXED;
  843. if (ccw_device_enable_console(cdev)) {
  844. ccw_device_destroy_console(cdev);
  845. raw3215_free_info(raw);
  846. raw3215[0] = NULL;
  847. return -ENODEV;
  848. }
  849. /* Request the console irq */
  850. if (raw3215_startup(raw) != 0) {
  851. raw3215_free_info(raw);
  852. raw3215[0] = NULL;
  853. return -ENODEV;
  854. }
  855. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  856. register_reboot_notifier(&on_reboot_nb);
  857. register_console(&con3215);
  858. return 0;
  859. }
  860. console_initcall(con3215_init);
  861. #endif
  862. static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
  863. {
  864. struct raw3215_info *raw;
  865. raw = raw3215[tty->index];
  866. if (raw == NULL)
  867. return -ENODEV;
  868. tty->driver_data = raw;
  869. return tty_port_install(&raw->port, driver, tty);
  870. }
  871. /*
  872. * tty3215_open
  873. *
  874. * This routine is called whenever a 3215 tty is opened.
  875. */
  876. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  877. {
  878. struct raw3215_info *raw = tty->driver_data;
  879. int retval;
  880. tty_port_tty_set(&raw->port, tty);
  881. raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
  882. /*
  883. * Start up 3215 device
  884. */
  885. retval = raw3215_startup(raw);
  886. if (retval)
  887. return retval;
  888. return 0;
  889. }
  890. /*
  891. * tty3215_close()
  892. *
  893. * This routine is called when the 3215 tty is closed. We wait
  894. * for the remaining request to be completed. Then we clean up.
  895. */
  896. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  897. {
  898. struct raw3215_info *raw;
  899. raw = (struct raw3215_info *) tty->driver_data;
  900. if (raw == NULL || tty->count > 1)
  901. return;
  902. tty->closing = 1;
  903. /* Shutdown the terminal */
  904. raw3215_shutdown(raw);
  905. tasklet_kill(&raw->tlet);
  906. tty->closing = 0;
  907. tty_port_tty_set(&raw->port, NULL);
  908. }
  909. /*
  910. * Returns the amount of free space in the output buffer.
  911. */
  912. static int tty3215_write_room(struct tty_struct *tty)
  913. {
  914. struct raw3215_info *raw;
  915. raw = (struct raw3215_info *) tty->driver_data;
  916. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  917. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  918. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  919. else
  920. return 0;
  921. }
  922. /*
  923. * String write routine for 3215 ttys
  924. */
  925. static int tty3215_write(struct tty_struct * tty,
  926. const unsigned char *buf, int count)
  927. {
  928. struct raw3215_info *raw;
  929. int i, written;
  930. if (!tty)
  931. return 0;
  932. raw = (struct raw3215_info *) tty->driver_data;
  933. written = count;
  934. while (count > 0) {
  935. for (i = 0; i < count; i++)
  936. if (buf[i] == '\t' || buf[i] == '\n')
  937. break;
  938. raw3215_write(raw, buf, i);
  939. count -= i;
  940. buf += i;
  941. if (count > 0) {
  942. raw3215_putchar(raw, *buf);
  943. count--;
  944. buf++;
  945. }
  946. }
  947. return written;
  948. }
  949. /*
  950. * Put character routine for 3215 ttys
  951. */
  952. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  953. {
  954. struct raw3215_info *raw;
  955. if (!tty)
  956. return 0;
  957. raw = (struct raw3215_info *) tty->driver_data;
  958. raw3215_putchar(raw, ch);
  959. return 1;
  960. }
  961. static void tty3215_flush_chars(struct tty_struct *tty)
  962. {
  963. }
  964. /*
  965. * Returns the number of characters in the output buffer
  966. */
  967. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  968. {
  969. struct raw3215_info *raw;
  970. raw = (struct raw3215_info *) tty->driver_data;
  971. return raw->count;
  972. }
  973. static void tty3215_flush_buffer(struct tty_struct *tty)
  974. {
  975. struct raw3215_info *raw;
  976. raw = (struct raw3215_info *) tty->driver_data;
  977. raw3215_flush_buffer(raw);
  978. tty_wakeup(tty);
  979. }
  980. /*
  981. * Disable reading from a 3215 tty
  982. */
  983. static void tty3215_throttle(struct tty_struct * tty)
  984. {
  985. struct raw3215_info *raw;
  986. raw = (struct raw3215_info *) tty->driver_data;
  987. raw->flags |= RAW3215_THROTTLED;
  988. }
  989. /*
  990. * Enable reading from a 3215 tty
  991. */
  992. static void tty3215_unthrottle(struct tty_struct * tty)
  993. {
  994. struct raw3215_info *raw;
  995. unsigned long flags;
  996. raw = (struct raw3215_info *) tty->driver_data;
  997. if (raw->flags & RAW3215_THROTTLED) {
  998. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  999. raw->flags &= ~RAW3215_THROTTLED;
  1000. raw3215_try_io(raw);
  1001. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1002. }
  1003. }
  1004. /*
  1005. * Disable writing to a 3215 tty
  1006. */
  1007. static void tty3215_stop(struct tty_struct *tty)
  1008. {
  1009. struct raw3215_info *raw;
  1010. raw = (struct raw3215_info *) tty->driver_data;
  1011. raw->flags |= RAW3215_STOPPED;
  1012. }
  1013. /*
  1014. * Enable writing to a 3215 tty
  1015. */
  1016. static void tty3215_start(struct tty_struct *tty)
  1017. {
  1018. struct raw3215_info *raw;
  1019. unsigned long flags;
  1020. raw = (struct raw3215_info *) tty->driver_data;
  1021. if (raw->flags & RAW3215_STOPPED) {
  1022. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  1023. raw->flags &= ~RAW3215_STOPPED;
  1024. raw3215_try_io(raw);
  1025. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1026. }
  1027. }
  1028. static const struct tty_operations tty3215_ops = {
  1029. .install = tty3215_install,
  1030. .open = tty3215_open,
  1031. .close = tty3215_close,
  1032. .write = tty3215_write,
  1033. .put_char = tty3215_put_char,
  1034. .flush_chars = tty3215_flush_chars,
  1035. .write_room = tty3215_write_room,
  1036. .chars_in_buffer = tty3215_chars_in_buffer,
  1037. .flush_buffer = tty3215_flush_buffer,
  1038. .throttle = tty3215_throttle,
  1039. .unthrottle = tty3215_unthrottle,
  1040. .stop = tty3215_stop,
  1041. .start = tty3215_start,
  1042. };
  1043. /*
  1044. * 3215 tty registration code called from tty_init().
  1045. * Most kernel services (incl. kmalloc) are available at this poimt.
  1046. */
  1047. static int __init tty3215_init(void)
  1048. {
  1049. struct tty_driver *driver;
  1050. int ret;
  1051. if (!CONSOLE_IS_3215)
  1052. return 0;
  1053. driver = alloc_tty_driver(NR_3215);
  1054. if (!driver)
  1055. return -ENOMEM;
  1056. ret = ccw_driver_register(&raw3215_ccw_driver);
  1057. if (ret) {
  1058. put_tty_driver(driver);
  1059. return ret;
  1060. }
  1061. /*
  1062. * Initialize the tty_driver structure
  1063. * Entries in tty3215_driver that are NOT initialized:
  1064. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1065. */
  1066. driver->driver_name = "tty3215";
  1067. driver->name = "ttyS";
  1068. driver->major = TTY_MAJOR;
  1069. driver->minor_start = 64;
  1070. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1071. driver->subtype = SYSTEM_TYPE_TTY;
  1072. driver->init_termios = tty_std_termios;
  1073. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1074. driver->init_termios.c_oflag = ONLCR;
  1075. driver->init_termios.c_lflag = ISIG;
  1076. driver->flags = TTY_DRIVER_REAL_RAW;
  1077. tty_set_operations(driver, &tty3215_ops);
  1078. ret = tty_register_driver(driver);
  1079. if (ret) {
  1080. put_tty_driver(driver);
  1081. return ret;
  1082. }
  1083. tty3215_driver = driver;
  1084. return 0;
  1085. }
  1086. device_initcall(tty3215_init);