con3215.c 30 KB

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