raw3270.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * IBM/3270 Driver - core functions.
  3. *
  4. * Author(s):
  5. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  6. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Copyright IBM Corp. 2003, 2009
  8. */
  9. #include <linux/kernel_stat.h>
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/wait.h>
  18. #include <asm/ccwdev.h>
  19. #include <asm/cio.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/diag.h>
  22. #include "raw3270.h"
  23. #include <linux/major.h>
  24. #include <linux/kdev_t.h>
  25. #include <linux/device.h>
  26. #include <linux/mutex.h>
  27. static struct class *class3270;
  28. /* The main 3270 data structure. */
  29. struct raw3270 {
  30. struct list_head list;
  31. struct ccw_device *cdev;
  32. int minor;
  33. short model, rows, cols;
  34. unsigned long flags;
  35. struct list_head req_queue; /* Request queue. */
  36. struct list_head view_list; /* List of available views. */
  37. struct raw3270_view *view; /* Active view. */
  38. struct timer_list timer; /* Device timer. */
  39. unsigned char *ascebc; /* ascii -> ebcdic table */
  40. struct device *clttydev; /* 3270-class tty device ptr */
  41. struct device *cltubdev; /* 3270-class tub device ptr */
  42. struct raw3270_request init_request;
  43. unsigned char init_data[256];
  44. };
  45. /* raw3270->flags */
  46. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  47. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  48. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  49. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  50. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  51. #define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
  52. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  53. static DEFINE_MUTEX(raw3270_mutex);
  54. /* List of 3270 devices. */
  55. static LIST_HEAD(raw3270_devices);
  56. /*
  57. * Flag to indicate if the driver has been registered. Some operations
  58. * like waiting for the end of i/o need to be done differently as long
  59. * as the kernel is still starting up (console support).
  60. */
  61. static int raw3270_registered;
  62. /* Module parameters */
  63. static int tubxcorrect = 0;
  64. module_param(tubxcorrect, bool, 0);
  65. /*
  66. * Wait queue for device init/delete, view delete.
  67. */
  68. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  69. /*
  70. * Encode array for 12 bit 3270 addresses.
  71. */
  72. static unsigned char raw3270_ebcgraf[64] = {
  73. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  74. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  75. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  76. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  77. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  78. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  79. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  80. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  81. };
  82. void
  83. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  84. {
  85. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  86. cp[0] = (addr >> 8) & 0x3f;
  87. cp[1] = addr & 0xff;
  88. } else {
  89. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  90. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  91. }
  92. }
  93. /*
  94. * Allocate a new 3270 ccw request
  95. */
  96. struct raw3270_request *
  97. raw3270_request_alloc(size_t size)
  98. {
  99. struct raw3270_request *rq;
  100. /* Allocate request structure */
  101. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  102. if (!rq)
  103. return ERR_PTR(-ENOMEM);
  104. /* alloc output buffer. */
  105. if (size > 0) {
  106. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  107. if (!rq->buffer) {
  108. kfree(rq);
  109. return ERR_PTR(-ENOMEM);
  110. }
  111. }
  112. rq->size = size;
  113. INIT_LIST_HEAD(&rq->list);
  114. /*
  115. * Setup ccw.
  116. */
  117. rq->ccw.cda = __pa(rq->buffer);
  118. rq->ccw.flags = CCW_FLAG_SLI;
  119. return rq;
  120. }
  121. /*
  122. * Free 3270 ccw request
  123. */
  124. void
  125. raw3270_request_free (struct raw3270_request *rq)
  126. {
  127. kfree(rq->buffer);
  128. kfree(rq);
  129. }
  130. /*
  131. * Reset request to initial state.
  132. */
  133. void
  134. raw3270_request_reset(struct raw3270_request *rq)
  135. {
  136. BUG_ON(!list_empty(&rq->list));
  137. rq->ccw.cmd_code = 0;
  138. rq->ccw.count = 0;
  139. rq->ccw.cda = __pa(rq->buffer);
  140. rq->ccw.flags = CCW_FLAG_SLI;
  141. rq->rescnt = 0;
  142. rq->rc = 0;
  143. }
  144. /*
  145. * Set command code to ccw of a request.
  146. */
  147. void
  148. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  149. {
  150. rq->ccw.cmd_code = cmd;
  151. }
  152. /*
  153. * Add data fragment to output buffer.
  154. */
  155. int
  156. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  157. {
  158. if (size + rq->ccw.count > rq->size)
  159. return -E2BIG;
  160. memcpy(rq->buffer + rq->ccw.count, data, size);
  161. rq->ccw.count += size;
  162. return 0;
  163. }
  164. /*
  165. * Set address/length pair to ccw of a request.
  166. */
  167. void
  168. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  169. {
  170. rq->ccw.cda = __pa(data);
  171. rq->ccw.count = size;
  172. }
  173. /*
  174. * Set idal buffer to ccw of a request.
  175. */
  176. void
  177. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  178. {
  179. rq->ccw.cda = __pa(ib->data);
  180. rq->ccw.count = ib->size;
  181. rq->ccw.flags |= CCW_FLAG_IDA;
  182. }
  183. /*
  184. * Stop running ccw.
  185. */
  186. static int
  187. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  188. {
  189. int retries;
  190. int rc;
  191. if (raw3270_request_final(rq))
  192. return 0;
  193. /* Check if interrupt has already been processed */
  194. for (retries = 0; retries < 5; retries++) {
  195. if (retries < 2)
  196. rc = ccw_device_halt(rp->cdev, (long) rq);
  197. else
  198. rc = ccw_device_clear(rp->cdev, (long) rq);
  199. if (rc == 0)
  200. break; /* termination successful */
  201. }
  202. return rc;
  203. }
  204. static int
  205. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  206. {
  207. unsigned long flags;
  208. int rc;
  209. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  210. rc = raw3270_halt_io_nolock(rp, rq);
  211. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  212. return rc;
  213. }
  214. /*
  215. * Add the request to the request queue, try to start it if the
  216. * 3270 device is idle. Return without waiting for end of i/o.
  217. */
  218. static int
  219. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  220. struct raw3270_request *rq)
  221. {
  222. rq->view = view;
  223. raw3270_get_view(view);
  224. if (list_empty(&rp->req_queue) &&
  225. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  226. /* No other requests are on the queue. Start this one. */
  227. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  228. (unsigned long) rq, 0, 0);
  229. if (rq->rc) {
  230. raw3270_put_view(view);
  231. return rq->rc;
  232. }
  233. }
  234. list_add_tail(&rq->list, &rp->req_queue);
  235. return 0;
  236. }
  237. int
  238. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  239. {
  240. unsigned long flags;
  241. struct raw3270 *rp;
  242. int rc;
  243. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  244. rp = view->dev;
  245. if (!rp || rp->view != view ||
  246. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  247. rc = -EACCES;
  248. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  249. rc = -ENODEV;
  250. else
  251. rc = __raw3270_start(rp, view, rq);
  252. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  253. return rc;
  254. }
  255. int
  256. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  257. {
  258. struct raw3270 *rp;
  259. int rc;
  260. rp = view->dev;
  261. if (!rp || rp->view != view ||
  262. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  263. rc = -EACCES;
  264. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  265. rc = -ENODEV;
  266. else
  267. rc = __raw3270_start(rp, view, rq);
  268. return rc;
  269. }
  270. int
  271. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  272. {
  273. struct raw3270 *rp;
  274. rp = view->dev;
  275. rq->view = view;
  276. raw3270_get_view(view);
  277. list_add_tail(&rq->list, &rp->req_queue);
  278. return 0;
  279. }
  280. /*
  281. * 3270 interrupt routine, called from the ccw_device layer
  282. */
  283. static void
  284. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  285. {
  286. struct raw3270 *rp;
  287. struct raw3270_view *view;
  288. struct raw3270_request *rq;
  289. int rc;
  290. kstat_cpu(smp_processor_id()).irqs[IOINT_C70]++;
  291. rp = dev_get_drvdata(&cdev->dev);
  292. if (!rp)
  293. return;
  294. rq = (struct raw3270_request *) intparm;
  295. view = rq ? rq->view : rp->view;
  296. if (IS_ERR(irb))
  297. rc = RAW3270_IO_RETRY;
  298. else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  299. rq->rc = -EIO;
  300. rc = RAW3270_IO_DONE;
  301. } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  302. DEV_STAT_UNIT_EXCEP)) {
  303. /* Handle CE-DE-UE and subsequent UDE */
  304. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  305. rc = RAW3270_IO_BUSY;
  306. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  307. /* Wait for UDE if busy flag is set. */
  308. if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  309. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  310. /* Got it, now retry. */
  311. rc = RAW3270_IO_RETRY;
  312. } else
  313. rc = RAW3270_IO_BUSY;
  314. } else if (view)
  315. rc = view->fn->intv(view, rq, irb);
  316. else
  317. rc = RAW3270_IO_DONE;
  318. switch (rc) {
  319. case RAW3270_IO_DONE:
  320. break;
  321. case RAW3270_IO_BUSY:
  322. /*
  323. * Intervention required by the operator. We have to wait
  324. * for unsolicited device end.
  325. */
  326. return;
  327. case RAW3270_IO_RETRY:
  328. if (!rq)
  329. break;
  330. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  331. (unsigned long) rq, 0, 0);
  332. if (rq->rc == 0)
  333. return; /* Successfully restarted. */
  334. break;
  335. case RAW3270_IO_STOP:
  336. if (!rq)
  337. break;
  338. raw3270_halt_io_nolock(rp, rq);
  339. rq->rc = -EIO;
  340. break;
  341. default:
  342. BUG();
  343. }
  344. if (rq) {
  345. BUG_ON(list_empty(&rq->list));
  346. /* The request completed, remove from queue and do callback. */
  347. list_del_init(&rq->list);
  348. if (rq->callback)
  349. rq->callback(rq, rq->callback_data);
  350. /* Do put_device for get_device in raw3270_start. */
  351. raw3270_put_view(view);
  352. }
  353. /*
  354. * Try to start each request on request queue until one is
  355. * started successful.
  356. */
  357. while (!list_empty(&rp->req_queue)) {
  358. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  359. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  360. (unsigned long) rq, 0, 0);
  361. if (rq->rc == 0)
  362. break;
  363. /* Start failed. Remove request and do callback. */
  364. list_del_init(&rq->list);
  365. if (rq->callback)
  366. rq->callback(rq, rq->callback_data);
  367. /* Do put_device for get_device in raw3270_start. */
  368. raw3270_put_view(view);
  369. }
  370. }
  371. /*
  372. * Size sensing.
  373. */
  374. struct raw3270_ua { /* Query Reply structure for Usable Area */
  375. struct { /* Usable Area Query Reply Base */
  376. short l; /* Length of this structured field */
  377. char sfid; /* 0x81 if Query Reply */
  378. char qcode; /* 0x81 if Usable Area */
  379. char flags0;
  380. char flags1;
  381. short w; /* Width of usable area */
  382. short h; /* Heigth of usavle area */
  383. char units; /* 0x00:in; 0x01:mm */
  384. int xr;
  385. int yr;
  386. char aw;
  387. char ah;
  388. short buffsz; /* Character buffer size, bytes */
  389. char xmin;
  390. char ymin;
  391. char xmax;
  392. char ymax;
  393. } __attribute__ ((packed)) uab;
  394. struct { /* Alternate Usable Area Self-Defining Parameter */
  395. char l; /* Length of this Self-Defining Parm */
  396. char sdpid; /* 0x02 if Alternate Usable Area */
  397. char res;
  398. char auaid; /* 0x01 is Id for the A U A */
  399. short wauai; /* Width of AUAi */
  400. short hauai; /* Height of AUAi */
  401. char auaunits; /* 0x00:in, 0x01:mm */
  402. int auaxr;
  403. int auayr;
  404. char awauai;
  405. char ahauai;
  406. } __attribute__ ((packed)) aua;
  407. } __attribute__ ((packed));
  408. static struct diag210 raw3270_init_diag210;
  409. static DEFINE_MUTEX(raw3270_init_mutex);
  410. static int
  411. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  412. struct irb *irb)
  413. {
  414. /*
  415. * Unit-Check Processing:
  416. * Expect Command Reject or Intervention Required.
  417. */
  418. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  419. /* Request finished abnormally. */
  420. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  421. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  422. return RAW3270_IO_BUSY;
  423. }
  424. }
  425. if (rq) {
  426. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  427. if (irb->ecw[0] & SNS0_CMD_REJECT)
  428. rq->rc = -EOPNOTSUPP;
  429. else
  430. rq->rc = -EIO;
  431. } else
  432. /* Request finished normally. Copy residual count. */
  433. rq->rescnt = irb->scsw.cmd.count;
  434. }
  435. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  436. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  437. wake_up(&raw3270_wait_queue);
  438. }
  439. return RAW3270_IO_DONE;
  440. }
  441. static struct raw3270_fn raw3270_init_fn = {
  442. .intv = raw3270_init_irq
  443. };
  444. static struct raw3270_view raw3270_init_view = {
  445. .fn = &raw3270_init_fn
  446. };
  447. /*
  448. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  449. * Wait for end of request. The request must have been started
  450. * with raw3270_start, rc = 0. The device lock may NOT have been
  451. * released between calling raw3270_start and raw3270_wait.
  452. */
  453. static void
  454. raw3270_wake_init(struct raw3270_request *rq, void *data)
  455. {
  456. wake_up((wait_queue_head_t *) data);
  457. }
  458. /*
  459. * Special wait function that can cope with console initialization.
  460. */
  461. static int
  462. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  463. struct raw3270_request *rq)
  464. {
  465. unsigned long flags;
  466. int rc;
  467. #ifdef CONFIG_TN3270_CONSOLE
  468. if (raw3270_registered == 0) {
  469. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  470. rq->callback = NULL;
  471. rc = __raw3270_start(rp, view, rq);
  472. if (rc == 0)
  473. while (!raw3270_request_final(rq)) {
  474. wait_cons_dev();
  475. barrier();
  476. }
  477. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  478. return rq->rc;
  479. }
  480. #endif
  481. rq->callback = raw3270_wake_init;
  482. rq->callback_data = &raw3270_wait_queue;
  483. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  484. rc = __raw3270_start(rp, view, rq);
  485. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  486. if (rc)
  487. return rc;
  488. /* Now wait for the completion. */
  489. rc = wait_event_interruptible(raw3270_wait_queue,
  490. raw3270_request_final(rq));
  491. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  492. raw3270_halt_io(view->dev, rq);
  493. /* No wait for the halt to complete. */
  494. wait_event(raw3270_wait_queue, raw3270_request_final(rq));
  495. return -ERESTARTSYS;
  496. }
  497. return rq->rc;
  498. }
  499. static int
  500. __raw3270_size_device_vm(struct raw3270 *rp)
  501. {
  502. int rc, model;
  503. struct ccw_dev_id dev_id;
  504. ccw_device_get_id(rp->cdev, &dev_id);
  505. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  506. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  507. rc = diag210(&raw3270_init_diag210);
  508. if (rc)
  509. return rc;
  510. model = raw3270_init_diag210.vrdccrmd;
  511. switch (model) {
  512. case 2:
  513. rp->model = model;
  514. rp->rows = 24;
  515. rp->cols = 80;
  516. break;
  517. case 3:
  518. rp->model = model;
  519. rp->rows = 32;
  520. rp->cols = 80;
  521. break;
  522. case 4:
  523. rp->model = model;
  524. rp->rows = 43;
  525. rp->cols = 80;
  526. break;
  527. case 5:
  528. rp->model = model;
  529. rp->rows = 27;
  530. rp->cols = 132;
  531. break;
  532. default:
  533. rc = -EOPNOTSUPP;
  534. break;
  535. }
  536. return rc;
  537. }
  538. static int
  539. __raw3270_size_device(struct raw3270 *rp)
  540. {
  541. static const unsigned char wbuf[] =
  542. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  543. struct raw3270_ua *uap;
  544. unsigned short count;
  545. int rc;
  546. /*
  547. * To determine the size of the 3270 device we need to do:
  548. * 1) send a 'read partition' data stream to the device
  549. * 2) wait for the attn interrupt that preceeds the query reply
  550. * 3) do a read modified to get the query reply
  551. * To make things worse we have to cope with intervention
  552. * required (3270 device switched to 'stand-by') and command
  553. * rejects (old devices that can't do 'read partition').
  554. */
  555. memset(&rp->init_request, 0, sizeof(rp->init_request));
  556. memset(&rp->init_data, 0, 256);
  557. /* Store 'read partition' data stream to init_data */
  558. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  559. INIT_LIST_HEAD(&rp->init_request.list);
  560. rp->init_request.ccw.cmd_code = TC_WRITESF;
  561. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  562. rp->init_request.ccw.count = sizeof(wbuf);
  563. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  564. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  565. if (rc)
  566. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  567. return rc;
  568. /* Wait for attention interrupt. */
  569. #ifdef CONFIG_TN3270_CONSOLE
  570. if (raw3270_registered == 0) {
  571. unsigned long flags;
  572. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  573. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  574. wait_cons_dev();
  575. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  576. } else
  577. #endif
  578. rc = wait_event_interruptible(raw3270_wait_queue,
  579. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  580. if (rc)
  581. return rc;
  582. /*
  583. * The device accepted the 'read partition' command. Now
  584. * set up a read ccw and issue it.
  585. */
  586. rp->init_request.ccw.cmd_code = TC_READMOD;
  587. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  588. rp->init_request.ccw.count = sizeof(rp->init_data);
  589. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  590. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  591. if (rc)
  592. return rc;
  593. /* Got a Query Reply */
  594. count = sizeof(rp->init_data) - rp->init_request.rescnt;
  595. uap = (struct raw3270_ua *) (rp->init_data + 1);
  596. /* Paranoia check. */
  597. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  598. return -EOPNOTSUPP;
  599. /* Copy rows/columns of default Usable Area */
  600. rp->rows = uap->uab.h;
  601. rp->cols = uap->uab.w;
  602. /* Check for 14 bit addressing */
  603. if ((uap->uab.flags0 & 0x0d) == 0x01)
  604. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  605. /* Check for Alternate Usable Area */
  606. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  607. uap->aua.sdpid == 0x02) {
  608. rp->rows = uap->aua.hauai;
  609. rp->cols = uap->aua.wauai;
  610. }
  611. return 0;
  612. }
  613. static int
  614. raw3270_size_device(struct raw3270 *rp)
  615. {
  616. int rc;
  617. mutex_lock(&raw3270_init_mutex);
  618. rp->view = &raw3270_init_view;
  619. raw3270_init_view.dev = rp;
  620. if (MACHINE_IS_VM)
  621. rc = __raw3270_size_device_vm(rp);
  622. else
  623. rc = __raw3270_size_device(rp);
  624. raw3270_init_view.dev = NULL;
  625. rp->view = NULL;
  626. mutex_unlock(&raw3270_init_mutex);
  627. if (rc == 0) { /* Found something. */
  628. /* Try to find a model. */
  629. rp->model = 0;
  630. if (rp->rows == 24 && rp->cols == 80)
  631. rp->model = 2;
  632. if (rp->rows == 32 && rp->cols == 80)
  633. rp->model = 3;
  634. if (rp->rows == 43 && rp->cols == 80)
  635. rp->model = 4;
  636. if (rp->rows == 27 && rp->cols == 132)
  637. rp->model = 5;
  638. } else {
  639. /* Couldn't detect size. Use default model 2. */
  640. rp->model = 2;
  641. rp->rows = 24;
  642. rp->cols = 80;
  643. return 0;
  644. }
  645. return rc;
  646. }
  647. static int
  648. raw3270_reset_device(struct raw3270 *rp)
  649. {
  650. int rc;
  651. mutex_lock(&raw3270_init_mutex);
  652. memset(&rp->init_request, 0, sizeof(rp->init_request));
  653. memset(&rp->init_data, 0, sizeof(rp->init_data));
  654. /* Store reset data stream to init_data/init_request */
  655. rp->init_data[0] = TW_KR;
  656. INIT_LIST_HEAD(&rp->init_request.list);
  657. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  658. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  659. rp->init_request.ccw.count = 1;
  660. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  661. rp->view = &raw3270_init_view;
  662. raw3270_init_view.dev = rp;
  663. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  664. raw3270_init_view.dev = NULL;
  665. rp->view = NULL;
  666. mutex_unlock(&raw3270_init_mutex);
  667. return rc;
  668. }
  669. int
  670. raw3270_reset(struct raw3270_view *view)
  671. {
  672. struct raw3270 *rp;
  673. int rc;
  674. rp = view->dev;
  675. if (!rp || rp->view != view ||
  676. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  677. rc = -EACCES;
  678. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  679. rc = -ENODEV;
  680. else
  681. rc = raw3270_reset_device(view->dev);
  682. return rc;
  683. }
  684. /*
  685. * Setup new 3270 device.
  686. */
  687. static int
  688. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  689. {
  690. struct list_head *l;
  691. struct raw3270 *tmp;
  692. int minor;
  693. memset(rp, 0, sizeof(struct raw3270));
  694. /* Copy ebcdic -> ascii translation table. */
  695. memcpy(ascebc, _ascebc, 256);
  696. if (tubxcorrect) {
  697. /* correct brackets and circumflex */
  698. ascebc['['] = 0xad;
  699. ascebc[']'] = 0xbd;
  700. ascebc['^'] = 0xb0;
  701. }
  702. rp->ascebc = ascebc;
  703. /* Set defaults. */
  704. rp->rows = 24;
  705. rp->cols = 80;
  706. INIT_LIST_HEAD(&rp->req_queue);
  707. INIT_LIST_HEAD(&rp->view_list);
  708. /*
  709. * Add device to list and find the smallest unused minor
  710. * number for it. Note: there is no device with minor 0,
  711. * see special case for fs3270.c:fs3270_open().
  712. */
  713. mutex_lock(&raw3270_mutex);
  714. /* Keep the list sorted. */
  715. minor = RAW3270_FIRSTMINOR;
  716. rp->minor = -1;
  717. list_for_each(l, &raw3270_devices) {
  718. tmp = list_entry(l, struct raw3270, list);
  719. if (tmp->minor > minor) {
  720. rp->minor = minor;
  721. __list_add(&rp->list, l->prev, l);
  722. break;
  723. }
  724. minor++;
  725. }
  726. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  727. rp->minor = minor;
  728. list_add_tail(&rp->list, &raw3270_devices);
  729. }
  730. mutex_unlock(&raw3270_mutex);
  731. /* No free minor number? Then give up. */
  732. if (rp->minor == -1)
  733. return -EUSERS;
  734. rp->cdev = cdev;
  735. dev_set_drvdata(&cdev->dev, rp);
  736. cdev->handler = raw3270_irq;
  737. return 0;
  738. }
  739. #ifdef CONFIG_TN3270_CONSOLE
  740. /*
  741. * Setup 3270 device configured as console.
  742. */
  743. struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
  744. {
  745. struct raw3270 *rp;
  746. char *ascebc;
  747. int rc;
  748. rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  749. ascebc = kzalloc(256, GFP_KERNEL);
  750. rc = raw3270_setup_device(cdev, rp, ascebc);
  751. if (rc)
  752. return ERR_PTR(rc);
  753. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  754. rc = raw3270_reset_device(rp);
  755. if (rc)
  756. return ERR_PTR(rc);
  757. rc = raw3270_size_device(rp);
  758. if (rc)
  759. return ERR_PTR(rc);
  760. rc = raw3270_reset_device(rp);
  761. if (rc)
  762. return ERR_PTR(rc);
  763. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  764. return rp;
  765. }
  766. void
  767. raw3270_wait_cons_dev(struct raw3270 *rp)
  768. {
  769. unsigned long flags;
  770. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  771. wait_cons_dev();
  772. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  773. }
  774. #endif
  775. /*
  776. * Create a 3270 device structure.
  777. */
  778. static struct raw3270 *
  779. raw3270_create_device(struct ccw_device *cdev)
  780. {
  781. struct raw3270 *rp;
  782. char *ascebc;
  783. int rc;
  784. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  785. if (!rp)
  786. return ERR_PTR(-ENOMEM);
  787. ascebc = kmalloc(256, GFP_KERNEL);
  788. if (!ascebc) {
  789. kfree(rp);
  790. return ERR_PTR(-ENOMEM);
  791. }
  792. rc = raw3270_setup_device(cdev, rp, ascebc);
  793. if (rc) {
  794. kfree(rp->ascebc);
  795. kfree(rp);
  796. rp = ERR_PTR(rc);
  797. }
  798. /* Get reference to ccw_device structure. */
  799. get_device(&cdev->dev);
  800. return rp;
  801. }
  802. /*
  803. * Activate a view.
  804. */
  805. int
  806. raw3270_activate_view(struct raw3270_view *view)
  807. {
  808. struct raw3270 *rp;
  809. struct raw3270_view *oldview, *nv;
  810. unsigned long flags;
  811. int rc;
  812. rp = view->dev;
  813. if (!rp)
  814. return -ENODEV;
  815. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  816. if (rp->view == view)
  817. rc = 0;
  818. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  819. rc = -ENODEV;
  820. else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  821. rc = -EACCES;
  822. else {
  823. oldview = NULL;
  824. if (rp->view) {
  825. oldview = rp->view;
  826. oldview->fn->deactivate(oldview);
  827. }
  828. rp->view = view;
  829. rc = view->fn->activate(view);
  830. if (rc) {
  831. /* Didn't work. Try to reactivate the old view. */
  832. rp->view = oldview;
  833. if (!oldview || oldview->fn->activate(oldview) != 0) {
  834. /* Didn't work as well. Try any other view. */
  835. list_for_each_entry(nv, &rp->view_list, list)
  836. if (nv != view && nv != oldview) {
  837. rp->view = nv;
  838. if (nv->fn->activate(nv) == 0)
  839. break;
  840. rp->view = NULL;
  841. }
  842. }
  843. }
  844. }
  845. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  846. return rc;
  847. }
  848. /*
  849. * Deactivate current view.
  850. */
  851. void
  852. raw3270_deactivate_view(struct raw3270_view *view)
  853. {
  854. unsigned long flags;
  855. struct raw3270 *rp;
  856. rp = view->dev;
  857. if (!rp)
  858. return;
  859. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  860. if (rp->view == view) {
  861. view->fn->deactivate(view);
  862. rp->view = NULL;
  863. /* Move deactivated view to end of list. */
  864. list_del_init(&view->list);
  865. list_add_tail(&view->list, &rp->view_list);
  866. /* Try to activate another view. */
  867. if (test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  868. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  869. list_for_each_entry(view, &rp->view_list, list) {
  870. rp->view = view;
  871. if (view->fn->activate(view) == 0)
  872. break;
  873. rp->view = NULL;
  874. }
  875. }
  876. }
  877. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  878. }
  879. /*
  880. * Add view to device with minor "minor".
  881. */
  882. int
  883. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  884. {
  885. unsigned long flags;
  886. struct raw3270 *rp;
  887. int rc;
  888. if (minor <= 0)
  889. return -ENODEV;
  890. mutex_lock(&raw3270_mutex);
  891. rc = -ENODEV;
  892. list_for_each_entry(rp, &raw3270_devices, list) {
  893. if (rp->minor != minor)
  894. continue;
  895. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  896. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  897. atomic_set(&view->ref_count, 2);
  898. view->dev = rp;
  899. view->fn = fn;
  900. view->model = rp->model;
  901. view->rows = rp->rows;
  902. view->cols = rp->cols;
  903. view->ascebc = rp->ascebc;
  904. spin_lock_init(&view->lock);
  905. list_add(&view->list, &rp->view_list);
  906. rc = 0;
  907. }
  908. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  909. break;
  910. }
  911. mutex_unlock(&raw3270_mutex);
  912. return rc;
  913. }
  914. /*
  915. * Find specific view of device with minor "minor".
  916. */
  917. struct raw3270_view *
  918. raw3270_find_view(struct raw3270_fn *fn, int minor)
  919. {
  920. struct raw3270 *rp;
  921. struct raw3270_view *view, *tmp;
  922. unsigned long flags;
  923. mutex_lock(&raw3270_mutex);
  924. view = ERR_PTR(-ENODEV);
  925. list_for_each_entry(rp, &raw3270_devices, list) {
  926. if (rp->minor != minor)
  927. continue;
  928. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  929. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  930. view = ERR_PTR(-ENOENT);
  931. list_for_each_entry(tmp, &rp->view_list, list) {
  932. if (tmp->fn == fn) {
  933. raw3270_get_view(tmp);
  934. view = tmp;
  935. break;
  936. }
  937. }
  938. }
  939. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  940. break;
  941. }
  942. mutex_unlock(&raw3270_mutex);
  943. return view;
  944. }
  945. /*
  946. * Remove view from device and free view structure via call to view->fn->free.
  947. */
  948. void
  949. raw3270_del_view(struct raw3270_view *view)
  950. {
  951. unsigned long flags;
  952. struct raw3270 *rp;
  953. struct raw3270_view *nv;
  954. rp = view->dev;
  955. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  956. if (rp->view == view) {
  957. view->fn->deactivate(view);
  958. rp->view = NULL;
  959. }
  960. list_del_init(&view->list);
  961. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  962. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  963. /* Try to activate another view. */
  964. list_for_each_entry(nv, &rp->view_list, list) {
  965. if (nv->fn->activate(nv) == 0) {
  966. rp->view = nv;
  967. break;
  968. }
  969. }
  970. }
  971. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  972. /* Wait for reference counter to drop to zero. */
  973. atomic_dec(&view->ref_count);
  974. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  975. if (view->fn->free)
  976. view->fn->free(view);
  977. }
  978. /*
  979. * Remove a 3270 device structure.
  980. */
  981. static void
  982. raw3270_delete_device(struct raw3270 *rp)
  983. {
  984. struct ccw_device *cdev;
  985. /* Remove from device chain. */
  986. mutex_lock(&raw3270_mutex);
  987. if (rp->clttydev && !IS_ERR(rp->clttydev))
  988. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  989. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  990. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
  991. list_del_init(&rp->list);
  992. mutex_unlock(&raw3270_mutex);
  993. /* Disconnect from ccw_device. */
  994. cdev = rp->cdev;
  995. rp->cdev = NULL;
  996. dev_set_drvdata(&cdev->dev, NULL);
  997. cdev->handler = NULL;
  998. /* Put ccw_device structure. */
  999. put_device(&cdev->dev);
  1000. /* Now free raw3270 structure. */
  1001. kfree(rp->ascebc);
  1002. kfree(rp);
  1003. }
  1004. static int
  1005. raw3270_probe (struct ccw_device *cdev)
  1006. {
  1007. return 0;
  1008. }
  1009. /*
  1010. * Additional attributes for a 3270 device
  1011. */
  1012. static ssize_t
  1013. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1014. {
  1015. return snprintf(buf, PAGE_SIZE, "%i\n",
  1016. ((struct raw3270 *) dev_get_drvdata(dev))->model);
  1017. }
  1018. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1019. static ssize_t
  1020. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1021. {
  1022. return snprintf(buf, PAGE_SIZE, "%i\n",
  1023. ((struct raw3270 *) dev_get_drvdata(dev))->rows);
  1024. }
  1025. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1026. static ssize_t
  1027. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1028. {
  1029. return snprintf(buf, PAGE_SIZE, "%i\n",
  1030. ((struct raw3270 *) dev_get_drvdata(dev))->cols);
  1031. }
  1032. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1033. static struct attribute * raw3270_attrs[] = {
  1034. &dev_attr_model.attr,
  1035. &dev_attr_rows.attr,
  1036. &dev_attr_columns.attr,
  1037. NULL,
  1038. };
  1039. static struct attribute_group raw3270_attr_group = {
  1040. .attrs = raw3270_attrs,
  1041. };
  1042. static int raw3270_create_attributes(struct raw3270 *rp)
  1043. {
  1044. int rc;
  1045. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1046. if (rc)
  1047. goto out;
  1048. rp->clttydev = device_create(class3270, &rp->cdev->dev,
  1049. MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
  1050. "tty%s", dev_name(&rp->cdev->dev));
  1051. if (IS_ERR(rp->clttydev)) {
  1052. rc = PTR_ERR(rp->clttydev);
  1053. goto out_ttydev;
  1054. }
  1055. rp->cltubdev = device_create(class3270, &rp->cdev->dev,
  1056. MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
  1057. "tub%s", dev_name(&rp->cdev->dev));
  1058. if (!IS_ERR(rp->cltubdev))
  1059. goto out;
  1060. rc = PTR_ERR(rp->cltubdev);
  1061. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1062. out_ttydev:
  1063. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1064. out:
  1065. return rc;
  1066. }
  1067. /*
  1068. * Notifier for device addition/removal
  1069. */
  1070. struct raw3270_notifier {
  1071. struct list_head list;
  1072. void (*notifier)(int, int);
  1073. };
  1074. static LIST_HEAD(raw3270_notifier);
  1075. int raw3270_register_notifier(void (*notifier)(int, int))
  1076. {
  1077. struct raw3270_notifier *np;
  1078. struct raw3270 *rp;
  1079. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1080. if (!np)
  1081. return -ENOMEM;
  1082. np->notifier = notifier;
  1083. mutex_lock(&raw3270_mutex);
  1084. list_add_tail(&np->list, &raw3270_notifier);
  1085. list_for_each_entry(rp, &raw3270_devices, list) {
  1086. get_device(&rp->cdev->dev);
  1087. notifier(rp->minor, 1);
  1088. }
  1089. mutex_unlock(&raw3270_mutex);
  1090. return 0;
  1091. }
  1092. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1093. {
  1094. struct raw3270_notifier *np;
  1095. mutex_lock(&raw3270_mutex);
  1096. list_for_each_entry(np, &raw3270_notifier, list)
  1097. if (np->notifier == notifier) {
  1098. list_del(&np->list);
  1099. kfree(np);
  1100. break;
  1101. }
  1102. mutex_unlock(&raw3270_mutex);
  1103. }
  1104. /*
  1105. * Set 3270 device online.
  1106. */
  1107. static int
  1108. raw3270_set_online (struct ccw_device *cdev)
  1109. {
  1110. struct raw3270 *rp;
  1111. struct raw3270_notifier *np;
  1112. int rc;
  1113. rp = raw3270_create_device(cdev);
  1114. if (IS_ERR(rp))
  1115. return PTR_ERR(rp);
  1116. rc = raw3270_reset_device(rp);
  1117. if (rc)
  1118. goto failure;
  1119. rc = raw3270_size_device(rp);
  1120. if (rc)
  1121. goto failure;
  1122. rc = raw3270_reset_device(rp);
  1123. if (rc)
  1124. goto failure;
  1125. rc = raw3270_create_attributes(rp);
  1126. if (rc)
  1127. goto failure;
  1128. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1129. mutex_lock(&raw3270_mutex);
  1130. list_for_each_entry(np, &raw3270_notifier, list)
  1131. np->notifier(rp->minor, 1);
  1132. mutex_unlock(&raw3270_mutex);
  1133. return 0;
  1134. failure:
  1135. raw3270_delete_device(rp);
  1136. return rc;
  1137. }
  1138. /*
  1139. * Remove 3270 device structure.
  1140. */
  1141. static void
  1142. raw3270_remove (struct ccw_device *cdev)
  1143. {
  1144. unsigned long flags;
  1145. struct raw3270 *rp;
  1146. struct raw3270_view *v;
  1147. struct raw3270_notifier *np;
  1148. rp = dev_get_drvdata(&cdev->dev);
  1149. /*
  1150. * _remove is the opposite of _probe; it's probe that
  1151. * should set up rp. raw3270_remove gets entered for
  1152. * devices even if they haven't been varied online.
  1153. * Thus, rp may validly be NULL here.
  1154. */
  1155. if (rp == NULL)
  1156. return;
  1157. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1158. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1159. /* Deactivate current view and remove all views. */
  1160. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1161. if (rp->view) {
  1162. rp->view->fn->deactivate(rp->view);
  1163. rp->view = NULL;
  1164. }
  1165. while (!list_empty(&rp->view_list)) {
  1166. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1167. if (v->fn->release)
  1168. v->fn->release(v);
  1169. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1170. raw3270_del_view(v);
  1171. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1172. }
  1173. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1174. mutex_lock(&raw3270_mutex);
  1175. list_for_each_entry(np, &raw3270_notifier, list)
  1176. np->notifier(rp->minor, 0);
  1177. mutex_unlock(&raw3270_mutex);
  1178. /* Reset 3270 device. */
  1179. raw3270_reset_device(rp);
  1180. /* And finally remove it. */
  1181. raw3270_delete_device(rp);
  1182. }
  1183. /*
  1184. * Set 3270 device offline.
  1185. */
  1186. static int
  1187. raw3270_set_offline (struct ccw_device *cdev)
  1188. {
  1189. struct raw3270 *rp;
  1190. rp = dev_get_drvdata(&cdev->dev);
  1191. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1192. return -EBUSY;
  1193. raw3270_remove(cdev);
  1194. return 0;
  1195. }
  1196. static int raw3270_pm_stop(struct ccw_device *cdev)
  1197. {
  1198. struct raw3270 *rp;
  1199. struct raw3270_view *view;
  1200. unsigned long flags;
  1201. rp = dev_get_drvdata(&cdev->dev);
  1202. if (!rp)
  1203. return 0;
  1204. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1205. if (rp->view)
  1206. rp->view->fn->deactivate(rp->view);
  1207. if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
  1208. /*
  1209. * Release tty and fullscreen for all non-console
  1210. * devices.
  1211. */
  1212. list_for_each_entry(view, &rp->view_list, list) {
  1213. if (view->fn->release)
  1214. view->fn->release(view);
  1215. }
  1216. }
  1217. set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1218. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1219. return 0;
  1220. }
  1221. static int raw3270_pm_start(struct ccw_device *cdev)
  1222. {
  1223. struct raw3270 *rp;
  1224. unsigned long flags;
  1225. rp = dev_get_drvdata(&cdev->dev);
  1226. if (!rp)
  1227. return 0;
  1228. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1229. clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1230. if (rp->view)
  1231. rp->view->fn->activate(rp->view);
  1232. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1233. return 0;
  1234. }
  1235. void raw3270_pm_unfreeze(struct raw3270_view *view)
  1236. {
  1237. #ifdef CONFIG_TN3270_CONSOLE
  1238. struct raw3270 *rp;
  1239. rp = view->dev;
  1240. if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  1241. ccw_device_force_console();
  1242. #endif
  1243. }
  1244. static struct ccw_device_id raw3270_id[] = {
  1245. { CCW_DEVICE(0x3270, 0) },
  1246. { CCW_DEVICE(0x3271, 0) },
  1247. { CCW_DEVICE(0x3272, 0) },
  1248. { CCW_DEVICE(0x3273, 0) },
  1249. { CCW_DEVICE(0x3274, 0) },
  1250. { CCW_DEVICE(0x3275, 0) },
  1251. { CCW_DEVICE(0x3276, 0) },
  1252. { CCW_DEVICE(0x3277, 0) },
  1253. { CCW_DEVICE(0x3278, 0) },
  1254. { CCW_DEVICE(0x3279, 0) },
  1255. { CCW_DEVICE(0x3174, 0) },
  1256. { /* end of list */ },
  1257. };
  1258. static struct ccw_driver raw3270_ccw_driver = {
  1259. .driver = {
  1260. .name = "3270",
  1261. .owner = THIS_MODULE,
  1262. },
  1263. .ids = raw3270_id,
  1264. .probe = &raw3270_probe,
  1265. .remove = &raw3270_remove,
  1266. .set_online = &raw3270_set_online,
  1267. .set_offline = &raw3270_set_offline,
  1268. .freeze = &raw3270_pm_stop,
  1269. .thaw = &raw3270_pm_start,
  1270. .restore = &raw3270_pm_start,
  1271. };
  1272. static int
  1273. raw3270_init(void)
  1274. {
  1275. struct raw3270 *rp;
  1276. int rc;
  1277. if (raw3270_registered)
  1278. return 0;
  1279. raw3270_registered = 1;
  1280. rc = ccw_driver_register(&raw3270_ccw_driver);
  1281. if (rc == 0) {
  1282. /* Create attributes for early (= console) device. */
  1283. mutex_lock(&raw3270_mutex);
  1284. class3270 = class_create(THIS_MODULE, "3270");
  1285. list_for_each_entry(rp, &raw3270_devices, list) {
  1286. get_device(&rp->cdev->dev);
  1287. raw3270_create_attributes(rp);
  1288. }
  1289. mutex_unlock(&raw3270_mutex);
  1290. }
  1291. return rc;
  1292. }
  1293. static void
  1294. raw3270_exit(void)
  1295. {
  1296. ccw_driver_unregister(&raw3270_ccw_driver);
  1297. class_destroy(class3270);
  1298. }
  1299. MODULE_LICENSE("GPL");
  1300. module_init(raw3270_init);
  1301. module_exit(raw3270_exit);
  1302. EXPORT_SYMBOL(raw3270_request_alloc);
  1303. EXPORT_SYMBOL(raw3270_request_free);
  1304. EXPORT_SYMBOL(raw3270_request_reset);
  1305. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1306. EXPORT_SYMBOL(raw3270_request_add_data);
  1307. EXPORT_SYMBOL(raw3270_request_set_data);
  1308. EXPORT_SYMBOL(raw3270_request_set_idal);
  1309. EXPORT_SYMBOL(raw3270_buffer_address);
  1310. EXPORT_SYMBOL(raw3270_add_view);
  1311. EXPORT_SYMBOL(raw3270_del_view);
  1312. EXPORT_SYMBOL(raw3270_find_view);
  1313. EXPORT_SYMBOL(raw3270_activate_view);
  1314. EXPORT_SYMBOL(raw3270_deactivate_view);
  1315. EXPORT_SYMBOL(raw3270_start);
  1316. EXPORT_SYMBOL(raw3270_start_locked);
  1317. EXPORT_SYMBOL(raw3270_start_irq);
  1318. EXPORT_SYMBOL(raw3270_reset);
  1319. EXPORT_SYMBOL(raw3270_register_notifier);
  1320. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1321. EXPORT_SYMBOL(raw3270_wait_queue);