con3215.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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_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 (!(raw->port.flags & ASYNC_SUSPENDED)) {
  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 (!(raw->port.flags & ASYNC_INITIALIZED) ||
  288. (raw->port.flags & ASYNC_SUSPENDED))
  289. return;
  290. if (raw->queued_read != NULL)
  291. raw3215_start_io(raw);
  292. else if (raw->queued_write != NULL) {
  293. if ((raw->queued_write->delayable == 0) ||
  294. (raw->flags & RAW3215_FLUSHING)) {
  295. /* execute write requests bigger than minimum size */
  296. raw3215_start_io(raw);
  297. }
  298. }
  299. if ((raw->queued_read || raw->queued_write) &&
  300. !(raw->flags & RAW3215_WORKING) &&
  301. !(raw->flags & RAW3215_TIMER_RUNS)) {
  302. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  303. add_timer(&raw->timer);
  304. raw->flags |= RAW3215_TIMER_RUNS;
  305. }
  306. }
  307. /*
  308. * Call tty_wakeup from tasklet context
  309. */
  310. static void raw3215_wakeup(unsigned long data)
  311. {
  312. struct raw3215_info *raw = (struct raw3215_info *) data;
  313. struct tty_struct *tty;
  314. tty = tty_port_tty_get(&raw->port);
  315. if (tty) {
  316. tty_wakeup(tty);
  317. tty_kref_put(tty);
  318. }
  319. }
  320. /*
  321. * Try to start the next IO and wake up processes waiting on the tty.
  322. */
  323. static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
  324. {
  325. raw3215_mk_write_req(raw);
  326. raw3215_try_io(raw);
  327. if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  328. tasklet_schedule(&raw->tlet);
  329. }
  330. /*
  331. * Interrupt routine, called from common io layer
  332. */
  333. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  334. struct irb *irb)
  335. {
  336. struct raw3215_info *raw;
  337. struct raw3215_req *req;
  338. struct tty_struct *tty;
  339. int cstat, dstat;
  340. int count;
  341. raw = dev_get_drvdata(&cdev->dev);
  342. req = (struct raw3215_req *) intparm;
  343. tty = tty_port_tty_get(&raw->port);
  344. cstat = irb->scsw.cmd.cstat;
  345. dstat = irb->scsw.cmd.dstat;
  346. if (cstat != 0)
  347. raw3215_next_io(raw, tty);
  348. if (dstat & 0x01) { /* we got a unit exception */
  349. dstat &= ~0x01; /* we can ignore it */
  350. }
  351. switch (dstat) {
  352. case 0x80:
  353. if (cstat != 0)
  354. break;
  355. /* Attention interrupt, someone hit the enter key */
  356. raw3215_mk_read_req(raw);
  357. raw3215_next_io(raw, tty);
  358. break;
  359. case 0x08:
  360. case 0x0C:
  361. /* Channel end interrupt. */
  362. if ((raw = req->info) == NULL)
  363. goto put_tty; /* That shouldn't happen ... */
  364. if (req->type == RAW3215_READ) {
  365. /* store residual count, then wait for device end */
  366. req->residual = irb->scsw.cmd.count;
  367. }
  368. if (dstat == 0x08)
  369. break;
  370. case 0x04:
  371. /* Device end interrupt. */
  372. if ((raw = req->info) == NULL)
  373. goto put_tty; /* That shouldn't happen ... */
  374. if (req->type == RAW3215_READ && tty != NULL) {
  375. unsigned int cchar;
  376. count = 160 - req->residual;
  377. EBCASC(raw->inbuf, count);
  378. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  379. switch (cchar & CTRLCHAR_MASK) {
  380. case CTRLCHAR_SYSRQ:
  381. break;
  382. case CTRLCHAR_CTRL:
  383. tty_insert_flip_char(&raw->port, cchar,
  384. TTY_NORMAL);
  385. tty_flip_buffer_push(&raw->port);
  386. break;
  387. case CTRLCHAR_NONE:
  388. if (count < 2 ||
  389. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  390. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  391. /* add the auto \n */
  392. raw->inbuf[count] = '\n';
  393. count++;
  394. } else
  395. count -= 2;
  396. tty_insert_flip_string(&raw->port, raw->inbuf,
  397. count);
  398. tty_flip_buffer_push(&raw->port);
  399. break;
  400. }
  401. } else if (req->type == RAW3215_WRITE) {
  402. raw->count -= req->len;
  403. raw->written -= req->len;
  404. }
  405. raw->flags &= ~RAW3215_WORKING;
  406. raw3215_free_req(req);
  407. /* check for empty wait */
  408. if (waitqueue_active(&raw->empty_wait) &&
  409. raw->queued_write == NULL &&
  410. raw->queued_read == NULL) {
  411. wake_up_interruptible(&raw->empty_wait);
  412. }
  413. raw3215_next_io(raw, tty);
  414. break;
  415. default:
  416. /* Strange interrupt, I'll do my best to clean up */
  417. if (req != NULL && req->type != RAW3215_FREE) {
  418. if (req->type == RAW3215_WRITE) {
  419. raw->count -= req->len;
  420. raw->written -= req->len;
  421. }
  422. raw->flags &= ~RAW3215_WORKING;
  423. raw3215_free_req(req);
  424. }
  425. raw3215_next_io(raw, tty);
  426. }
  427. put_tty:
  428. tty_kref_put(tty);
  429. }
  430. /*
  431. * Drop the oldest line from the output buffer.
  432. */
  433. static void raw3215_drop_line(struct raw3215_info *raw)
  434. {
  435. int ix;
  436. char ch;
  437. BUG_ON(raw->written != 0);
  438. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  439. while (raw->count > 0) {
  440. ch = raw->buffer[ix];
  441. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  442. raw->count--;
  443. if (ch == 0x15)
  444. break;
  445. }
  446. raw->head = ix;
  447. }
  448. /*
  449. * Wait until length bytes are available int the output buffer.
  450. * Has to be called with the s390irq lock held. Can be called
  451. * disabled.
  452. */
  453. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  454. {
  455. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  456. /* While console is frozen for suspend we have no other
  457. * choice but to drop message from the buffer to make
  458. * room for even more messages. */
  459. if (raw->port.flags & ASYNC_SUSPENDED) {
  460. raw3215_drop_line(raw);
  461. continue;
  462. }
  463. /* there might be a request pending */
  464. raw->flags |= RAW3215_FLUSHING;
  465. raw3215_mk_write_req(raw);
  466. raw3215_try_io(raw);
  467. raw->flags &= ~RAW3215_FLUSHING;
  468. #ifdef CONFIG_TN3215_CONSOLE
  469. ccw_device_wait_idle(raw->cdev);
  470. #endif
  471. /* Enough room freed up ? */
  472. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  473. break;
  474. /* there might be another cpu waiting for the lock */
  475. spin_unlock(get_ccwdev_lock(raw->cdev));
  476. udelay(100);
  477. spin_lock(get_ccwdev_lock(raw->cdev));
  478. }
  479. }
  480. /*
  481. * String write routine for 3215 devices
  482. */
  483. static void raw3215_write(struct raw3215_info *raw, const char *str,
  484. unsigned int length)
  485. {
  486. unsigned long flags;
  487. int c, count;
  488. while (length > 0) {
  489. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  490. count = (length > RAW3215_BUFFER_SIZE) ?
  491. RAW3215_BUFFER_SIZE : length;
  492. length -= count;
  493. raw3215_make_room(raw, count);
  494. /* copy string to output buffer and convert it to EBCDIC */
  495. while (1) {
  496. c = min_t(int, count,
  497. min(RAW3215_BUFFER_SIZE - raw->count,
  498. RAW3215_BUFFER_SIZE - raw->head));
  499. if (c <= 0)
  500. break;
  501. memcpy(raw->buffer + raw->head, str, c);
  502. ASCEBC(raw->buffer + raw->head, c);
  503. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  504. raw->count += c;
  505. raw->line_pos += c;
  506. str += c;
  507. count -= c;
  508. }
  509. if (!(raw->flags & RAW3215_WORKING)) {
  510. raw3215_mk_write_req(raw);
  511. /* start or queue request */
  512. raw3215_try_io(raw);
  513. }
  514. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  515. }
  516. }
  517. /*
  518. * Put character routine for 3215 devices
  519. */
  520. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  521. {
  522. unsigned long flags;
  523. unsigned int length, i;
  524. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  525. if (ch == '\t') {
  526. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  527. raw->line_pos += length;
  528. ch = ' ';
  529. } else if (ch == '\n') {
  530. length = 1;
  531. raw->line_pos = 0;
  532. } else {
  533. length = 1;
  534. raw->line_pos++;
  535. }
  536. raw3215_make_room(raw, length);
  537. for (i = 0; i < length; i++) {
  538. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  539. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  540. raw->count++;
  541. }
  542. if (!(raw->flags & RAW3215_WORKING)) {
  543. raw3215_mk_write_req(raw);
  544. /* start or queue request */
  545. raw3215_try_io(raw);
  546. }
  547. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  548. }
  549. /*
  550. * Flush routine, it simply sets the flush flag and tries to start
  551. * pending IO.
  552. */
  553. static void raw3215_flush_buffer(struct raw3215_info *raw)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  557. if (raw->count > 0) {
  558. raw->flags |= RAW3215_FLUSHING;
  559. raw3215_try_io(raw);
  560. raw->flags &= ~RAW3215_FLUSHING;
  561. }
  562. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  563. }
  564. /*
  565. * Fire up a 3215 device.
  566. */
  567. static int raw3215_startup(struct raw3215_info *raw)
  568. {
  569. unsigned long flags;
  570. if (raw->port.flags & ASYNC_INITIALIZED)
  571. return 0;
  572. raw->line_pos = 0;
  573. raw->port.flags |= ASYNC_INITIALIZED;
  574. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  575. raw3215_try_io(raw);
  576. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  577. return 0;
  578. }
  579. /*
  580. * Shutdown a 3215 device.
  581. */
  582. static void raw3215_shutdown(struct raw3215_info *raw)
  583. {
  584. DECLARE_WAITQUEUE(wait, current);
  585. unsigned long flags;
  586. if (!(raw->port.flags & ASYNC_INITIALIZED) ||
  587. (raw->flags & RAW3215_FIXED))
  588. return;
  589. /* Wait for outstanding requests, then free irq */
  590. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  591. if ((raw->flags & RAW3215_WORKING) ||
  592. raw->queued_write != NULL ||
  593. raw->queued_read != NULL) {
  594. raw->port.flags |= ASYNC_CLOSING;
  595. add_wait_queue(&raw->empty_wait, &wait);
  596. set_current_state(TASK_INTERRUPTIBLE);
  597. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  598. schedule();
  599. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  600. remove_wait_queue(&raw->empty_wait, &wait);
  601. set_current_state(TASK_RUNNING);
  602. raw->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING);
  603. }
  604. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  605. }
  606. static struct raw3215_info *raw3215_alloc_info(void)
  607. {
  608. struct raw3215_info *info;
  609. info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  610. if (!info)
  611. return NULL;
  612. info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  613. info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  614. if (!info->buffer || !info->inbuf) {
  615. kfree(info);
  616. return NULL;
  617. }
  618. setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
  619. init_waitqueue_head(&info->empty_wait);
  620. tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
  621. tty_port_init(&info->port);
  622. return info;
  623. }
  624. static void raw3215_free_info(struct raw3215_info *raw)
  625. {
  626. kfree(raw->inbuf);
  627. kfree(raw->buffer);
  628. tty_port_destroy(&raw->port);
  629. kfree(raw);
  630. }
  631. static int raw3215_probe (struct ccw_device *cdev)
  632. {
  633. struct raw3215_info *raw;
  634. int line;
  635. /* Console is special. */
  636. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  637. return 0;
  638. raw = raw3215_alloc_info();
  639. if (raw == NULL)
  640. return -ENOMEM;
  641. raw->cdev = cdev;
  642. dev_set_drvdata(&cdev->dev, raw);
  643. cdev->handler = raw3215_irq;
  644. spin_lock(&raw3215_device_lock);
  645. for (line = 0; line < NR_3215; line++) {
  646. if (!raw3215[line]) {
  647. raw3215[line] = raw;
  648. break;
  649. }
  650. }
  651. spin_unlock(&raw3215_device_lock);
  652. if (line == NR_3215) {
  653. raw3215_free_info(raw);
  654. return -ENODEV;
  655. }
  656. return 0;
  657. }
  658. static void raw3215_remove (struct ccw_device *cdev)
  659. {
  660. struct raw3215_info *raw;
  661. unsigned int line;
  662. ccw_device_set_offline(cdev);
  663. raw = dev_get_drvdata(&cdev->dev);
  664. if (raw) {
  665. spin_lock(&raw3215_device_lock);
  666. for (line = 0; line < NR_3215; line++)
  667. if (raw3215[line] == raw)
  668. break;
  669. raw3215[line] = NULL;
  670. spin_unlock(&raw3215_device_lock);
  671. dev_set_drvdata(&cdev->dev, NULL);
  672. raw3215_free_info(raw);
  673. }
  674. }
  675. static int raw3215_set_online (struct ccw_device *cdev)
  676. {
  677. struct raw3215_info *raw;
  678. raw = dev_get_drvdata(&cdev->dev);
  679. if (!raw)
  680. return -ENODEV;
  681. return raw3215_startup(raw);
  682. }
  683. static int raw3215_set_offline (struct ccw_device *cdev)
  684. {
  685. struct raw3215_info *raw;
  686. raw = dev_get_drvdata(&cdev->dev);
  687. if (!raw)
  688. return -ENODEV;
  689. raw3215_shutdown(raw);
  690. return 0;
  691. }
  692. static int raw3215_pm_stop(struct ccw_device *cdev)
  693. {
  694. struct raw3215_info *raw;
  695. unsigned long flags;
  696. /* Empty the output buffer, then prevent new I/O. */
  697. raw = dev_get_drvdata(&cdev->dev);
  698. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  699. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  700. raw->port.flags |= ASYNC_SUSPENDED;
  701. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  702. return 0;
  703. }
  704. static int raw3215_pm_start(struct ccw_device *cdev)
  705. {
  706. struct raw3215_info *raw;
  707. unsigned long flags;
  708. /* Allow I/O again and flush output buffer. */
  709. raw = dev_get_drvdata(&cdev->dev);
  710. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  711. raw->port.flags &= ~ASYNC_SUSPENDED;
  712. raw->flags |= RAW3215_FLUSHING;
  713. raw3215_try_io(raw);
  714. raw->flags &= ~RAW3215_FLUSHING;
  715. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  716. return 0;
  717. }
  718. static struct ccw_device_id raw3215_id[] = {
  719. { CCW_DEVICE(0x3215, 0) },
  720. { /* end of list */ },
  721. };
  722. static struct ccw_driver raw3215_ccw_driver = {
  723. .driver = {
  724. .name = "3215",
  725. .owner = THIS_MODULE,
  726. },
  727. .ids = raw3215_id,
  728. .probe = &raw3215_probe,
  729. .remove = &raw3215_remove,
  730. .set_online = &raw3215_set_online,
  731. .set_offline = &raw3215_set_offline,
  732. .freeze = &raw3215_pm_stop,
  733. .thaw = &raw3215_pm_start,
  734. .restore = &raw3215_pm_start,
  735. .int_class = IRQIO_C15,
  736. };
  737. #ifdef CONFIG_TN3215_CONSOLE
  738. /*
  739. * Write a string to the 3215 console
  740. */
  741. static void con3215_write(struct console *co, const char *str,
  742. unsigned int count)
  743. {
  744. struct raw3215_info *raw;
  745. int i;
  746. if (count <= 0)
  747. return;
  748. raw = raw3215[0]; /* console 3215 is the first one */
  749. while (count > 0) {
  750. for (i = 0; i < count; i++)
  751. if (str[i] == '\t' || str[i] == '\n')
  752. break;
  753. raw3215_write(raw, str, i);
  754. count -= i;
  755. str += i;
  756. if (count > 0) {
  757. raw3215_putchar(raw, *str);
  758. count--;
  759. str++;
  760. }
  761. }
  762. }
  763. static struct tty_driver *con3215_device(struct console *c, int *index)
  764. {
  765. *index = c->index;
  766. return tty3215_driver;
  767. }
  768. /*
  769. * panic() calls con3215_flush through a panic_notifier
  770. * before the system enters a disabled, endless loop.
  771. */
  772. static void con3215_flush(void)
  773. {
  774. struct raw3215_info *raw;
  775. unsigned long flags;
  776. raw = raw3215[0]; /* console 3215 is the first one */
  777. if (raw->port.flags & ASYNC_SUSPENDED)
  778. /* The console is still frozen for suspend. */
  779. if (ccw_device_force_console(raw->cdev))
  780. /* Forcing didn't work, no panic message .. */
  781. return;
  782. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  783. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  784. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  785. }
  786. static int con3215_notify(struct notifier_block *self,
  787. unsigned long event, void *data)
  788. {
  789. con3215_flush();
  790. return NOTIFY_OK;
  791. }
  792. static struct notifier_block on_panic_nb = {
  793. .notifier_call = con3215_notify,
  794. .priority = 0,
  795. };
  796. static struct notifier_block on_reboot_nb = {
  797. .notifier_call = con3215_notify,
  798. .priority = 0,
  799. };
  800. /*
  801. * The console structure for the 3215 console
  802. */
  803. static struct console con3215 = {
  804. .name = "ttyS",
  805. .write = con3215_write,
  806. .device = con3215_device,
  807. .flags = CON_PRINTBUFFER,
  808. };
  809. /*
  810. * 3215 console initialization code called from console_init().
  811. */
  812. static int __init con3215_init(void)
  813. {
  814. struct ccw_device *cdev;
  815. struct raw3215_info *raw;
  816. struct raw3215_req *req;
  817. int i;
  818. /* Check if 3215 is to be the console */
  819. if (!CONSOLE_IS_3215)
  820. return -ENODEV;
  821. /* Set the console mode for VM */
  822. if (MACHINE_IS_VM) {
  823. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  824. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  825. }
  826. /* allocate 3215 request structures */
  827. raw3215_freelist = NULL;
  828. spin_lock_init(&raw3215_freelist_lock);
  829. for (i = 0; i < NR_3215_REQ; i++) {
  830. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  831. req->next = raw3215_freelist;
  832. raw3215_freelist = req;
  833. }
  834. cdev = ccw_device_create_console(&raw3215_ccw_driver);
  835. if (IS_ERR(cdev))
  836. return -ENODEV;
  837. raw3215[0] = raw = raw3215_alloc_info();
  838. raw->cdev = cdev;
  839. dev_set_drvdata(&cdev->dev, raw);
  840. cdev->handler = raw3215_irq;
  841. raw->flags |= RAW3215_FIXED;
  842. if (ccw_device_enable_console(cdev)) {
  843. ccw_device_destroy_console(cdev);
  844. raw3215_free_info(raw);
  845. raw3215[0] = NULL;
  846. return -ENODEV;
  847. }
  848. /* Request the console irq */
  849. if (raw3215_startup(raw) != 0) {
  850. raw3215_free_info(raw);
  851. raw3215[0] = NULL;
  852. return -ENODEV;
  853. }
  854. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  855. register_reboot_notifier(&on_reboot_nb);
  856. register_console(&con3215);
  857. return 0;
  858. }
  859. console_initcall(con3215_init);
  860. #endif
  861. static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
  862. {
  863. struct raw3215_info *raw;
  864. raw = raw3215[tty->index];
  865. if (raw == NULL)
  866. return -ENODEV;
  867. tty->driver_data = raw;
  868. return tty_port_install(&raw->port, driver, tty);
  869. }
  870. /*
  871. * tty3215_open
  872. *
  873. * This routine is called whenever a 3215 tty is opened.
  874. */
  875. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  876. {
  877. struct raw3215_info *raw = tty->driver_data;
  878. int retval;
  879. tty_port_tty_set(&raw->port, tty);
  880. raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
  881. /*
  882. * Start up 3215 device
  883. */
  884. retval = raw3215_startup(raw);
  885. if (retval)
  886. return retval;
  887. return 0;
  888. }
  889. /*
  890. * tty3215_close()
  891. *
  892. * This routine is called when the 3215 tty is closed. We wait
  893. * for the remaining request to be completed. Then we clean up.
  894. */
  895. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  896. {
  897. struct raw3215_info *raw;
  898. raw = (struct raw3215_info *) tty->driver_data;
  899. if (raw == NULL || tty->count > 1)
  900. return;
  901. tty->closing = 1;
  902. /* Shutdown the terminal */
  903. raw3215_shutdown(raw);
  904. tasklet_kill(&raw->tlet);
  905. tty->closing = 0;
  906. tty_port_tty_set(&raw->port, NULL);
  907. }
  908. /*
  909. * Returns the amount of free space in the output buffer.
  910. */
  911. static int tty3215_write_room(struct tty_struct *tty)
  912. {
  913. struct raw3215_info *raw;
  914. raw = (struct raw3215_info *) tty->driver_data;
  915. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  916. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  917. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  918. else
  919. return 0;
  920. }
  921. /*
  922. * String write routine for 3215 ttys
  923. */
  924. static int tty3215_write(struct tty_struct * tty,
  925. const unsigned char *buf, int count)
  926. {
  927. struct raw3215_info *raw;
  928. int i, written;
  929. if (!tty)
  930. return 0;
  931. raw = (struct raw3215_info *) tty->driver_data;
  932. written = count;
  933. while (count > 0) {
  934. for (i = 0; i < count; i++)
  935. if (buf[i] == '\t' || buf[i] == '\n')
  936. break;
  937. raw3215_write(raw, buf, i);
  938. count -= i;
  939. buf += i;
  940. if (count > 0) {
  941. raw3215_putchar(raw, *buf);
  942. count--;
  943. buf++;
  944. }
  945. }
  946. return written;
  947. }
  948. /*
  949. * Put character routine for 3215 ttys
  950. */
  951. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  952. {
  953. struct raw3215_info *raw;
  954. if (!tty)
  955. return 0;
  956. raw = (struct raw3215_info *) tty->driver_data;
  957. raw3215_putchar(raw, ch);
  958. return 1;
  959. }
  960. static void tty3215_flush_chars(struct tty_struct *tty)
  961. {
  962. }
  963. /*
  964. * Returns the number of characters in the output buffer
  965. */
  966. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  967. {
  968. struct raw3215_info *raw;
  969. raw = (struct raw3215_info *) tty->driver_data;
  970. return raw->count;
  971. }
  972. static void tty3215_flush_buffer(struct tty_struct *tty)
  973. {
  974. struct raw3215_info *raw;
  975. raw = (struct raw3215_info *) tty->driver_data;
  976. raw3215_flush_buffer(raw);
  977. tty_wakeup(tty);
  978. }
  979. /*
  980. * Disable reading from a 3215 tty
  981. */
  982. static void tty3215_throttle(struct tty_struct * tty)
  983. {
  984. struct raw3215_info *raw;
  985. raw = (struct raw3215_info *) tty->driver_data;
  986. raw->flags |= RAW3215_THROTTLED;
  987. }
  988. /*
  989. * Enable reading from a 3215 tty
  990. */
  991. static void tty3215_unthrottle(struct tty_struct * tty)
  992. {
  993. struct raw3215_info *raw;
  994. unsigned long flags;
  995. raw = (struct raw3215_info *) tty->driver_data;
  996. if (raw->flags & RAW3215_THROTTLED) {
  997. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  998. raw->flags &= ~RAW3215_THROTTLED;
  999. raw3215_try_io(raw);
  1000. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1001. }
  1002. }
  1003. /*
  1004. * Disable writing to a 3215 tty
  1005. */
  1006. static void tty3215_stop(struct tty_struct *tty)
  1007. {
  1008. struct raw3215_info *raw;
  1009. raw = (struct raw3215_info *) tty->driver_data;
  1010. raw->flags |= RAW3215_STOPPED;
  1011. }
  1012. /*
  1013. * Enable writing to a 3215 tty
  1014. */
  1015. static void tty3215_start(struct tty_struct *tty)
  1016. {
  1017. struct raw3215_info *raw;
  1018. unsigned long flags;
  1019. raw = (struct raw3215_info *) tty->driver_data;
  1020. if (raw->flags & RAW3215_STOPPED) {
  1021. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  1022. raw->flags &= ~RAW3215_STOPPED;
  1023. raw3215_try_io(raw);
  1024. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1025. }
  1026. }
  1027. static const struct tty_operations tty3215_ops = {
  1028. .install = tty3215_install,
  1029. .open = tty3215_open,
  1030. .close = tty3215_close,
  1031. .write = tty3215_write,
  1032. .put_char = tty3215_put_char,
  1033. .flush_chars = tty3215_flush_chars,
  1034. .write_room = tty3215_write_room,
  1035. .chars_in_buffer = tty3215_chars_in_buffer,
  1036. .flush_buffer = tty3215_flush_buffer,
  1037. .throttle = tty3215_throttle,
  1038. .unthrottle = tty3215_unthrottle,
  1039. .stop = tty3215_stop,
  1040. .start = tty3215_start,
  1041. };
  1042. /*
  1043. * 3215 tty registration code called from tty_init().
  1044. * Most kernel services (incl. kmalloc) are available at this poimt.
  1045. */
  1046. static int __init tty3215_init(void)
  1047. {
  1048. struct tty_driver *driver;
  1049. int ret;
  1050. if (!CONSOLE_IS_3215)
  1051. return 0;
  1052. driver = alloc_tty_driver(NR_3215);
  1053. if (!driver)
  1054. return -ENOMEM;
  1055. ret = ccw_driver_register(&raw3215_ccw_driver);
  1056. if (ret) {
  1057. put_tty_driver(driver);
  1058. return ret;
  1059. }
  1060. /*
  1061. * Initialize the tty_driver structure
  1062. * Entries in tty3215_driver that are NOT initialized:
  1063. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1064. */
  1065. driver->driver_name = "tty3215";
  1066. driver->name = "ttyS";
  1067. driver->major = TTY_MAJOR;
  1068. driver->minor_start = 64;
  1069. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1070. driver->subtype = SYSTEM_TYPE_TTY;
  1071. driver->init_termios = tty_std_termios;
  1072. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1073. driver->init_termios.c_oflag = ONLCR;
  1074. driver->init_termios.c_lflag = ISIG;
  1075. driver->flags = TTY_DRIVER_REAL_RAW;
  1076. tty_set_operations(driver, &tty3215_ops);
  1077. ret = tty_register_driver(driver);
  1078. if (ret) {
  1079. put_tty_driver(driver);
  1080. return ret;
  1081. }
  1082. tty3215_driver = driver;
  1083. return 0;
  1084. }
  1085. static void __exit tty3215_exit(void)
  1086. {
  1087. tty_unregister_driver(tty3215_driver);
  1088. put_tty_driver(tty3215_driver);
  1089. ccw_driver_unregister(&raw3215_ccw_driver);
  1090. }
  1091. module_init(tty3215_init);
  1092. module_exit(tty3215_exit);