raw3270.c 35 KB

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