paride.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. paride.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. This is the base module for the family of device drivers
  5. that support parallel port IDE devices.
  6. */
  7. /* Changes:
  8. 1.01 GRG 1998.05.03 Use spinlocks
  9. 1.02 GRG 1998.05.05 init_proto, release_proto, ktti
  10. 1.03 GRG 1998.08.15 eliminate compiler warning
  11. 1.04 GRG 1998.11.28 added support for FRIQ
  12. 1.05 TMW 2000.06.06 use parport_find_number instead of
  13. parport_enumerate
  14. 1.06 TMW 2001.03.26 more sane parport-or-not resource management
  15. */
  16. #define PI_VERSION "1.06"
  17. #include <linux/module.h>
  18. #include <linux/config.h>
  19. #include <linux/kmod.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ioport.h>
  23. #include <linux/string.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/wait.h>
  26. #include <linux/sched.h> /* TASK_* */
  27. #ifdef CONFIG_PARPORT_MODULE
  28. #define CONFIG_PARPORT
  29. #endif
  30. #ifdef CONFIG_PARPORT
  31. #include <linux/parport.h>
  32. #endif
  33. #include "paride.h"
  34. MODULE_LICENSE("GPL");
  35. #define MAX_PROTOS 32
  36. static struct pi_protocol *protocols[MAX_PROTOS];
  37. static DEFINE_SPINLOCK(pi_spinlock);
  38. void pi_write_regr(PIA * pi, int cont, int regr, int val)
  39. {
  40. pi->proto->write_regr(pi, cont, regr, val);
  41. }
  42. EXPORT_SYMBOL(pi_write_regr);
  43. int pi_read_regr(PIA * pi, int cont, int regr)
  44. {
  45. return pi->proto->read_regr(pi, cont, regr);
  46. }
  47. EXPORT_SYMBOL(pi_read_regr);
  48. void pi_write_block(PIA * pi, char *buf, int count)
  49. {
  50. pi->proto->write_block(pi, buf, count);
  51. }
  52. EXPORT_SYMBOL(pi_write_block);
  53. void pi_read_block(PIA * pi, char *buf, int count)
  54. {
  55. pi->proto->read_block(pi, buf, count);
  56. }
  57. EXPORT_SYMBOL(pi_read_block);
  58. #ifdef CONFIG_PARPORT
  59. static void pi_wake_up(void *p)
  60. {
  61. PIA *pi = (PIA *) p;
  62. unsigned long flags;
  63. void (*cont) (void) = NULL;
  64. spin_lock_irqsave(&pi_spinlock, flags);
  65. if (pi->claim_cont && !parport_claim(pi->pardev)) {
  66. cont = pi->claim_cont;
  67. pi->claim_cont = NULL;
  68. pi->claimed = 1;
  69. }
  70. spin_unlock_irqrestore(&pi_spinlock, flags);
  71. wake_up(&(pi->parq));
  72. if (cont)
  73. cont();
  74. }
  75. #endif
  76. int pi_schedule_claimed(PIA * pi, void (*cont) (void))
  77. {
  78. #ifdef CONFIG_PARPORT
  79. unsigned long flags;
  80. spin_lock_irqsave(&pi_spinlock, flags);
  81. if (pi->pardev && parport_claim(pi->pardev)) {
  82. pi->claim_cont = cont;
  83. spin_unlock_irqrestore(&pi_spinlock, flags);
  84. return 0;
  85. }
  86. pi->claimed = 1;
  87. spin_unlock_irqrestore(&pi_spinlock, flags);
  88. #endif
  89. return 1;
  90. }
  91. EXPORT_SYMBOL(pi_schedule_claimed);
  92. void pi_do_claimed(PIA * pi, void (*cont) (void))
  93. {
  94. if (pi_schedule_claimed(pi, cont))
  95. cont();
  96. }
  97. EXPORT_SYMBOL(pi_do_claimed);
  98. static void pi_claim(PIA * pi)
  99. {
  100. if (pi->claimed)
  101. return;
  102. pi->claimed = 1;
  103. #ifdef CONFIG_PARPORT
  104. if (pi->pardev)
  105. wait_event(pi->parq,
  106. !parport_claim((struct pardevice *) pi->pardev));
  107. #endif
  108. }
  109. static void pi_unclaim(PIA * pi)
  110. {
  111. pi->claimed = 0;
  112. #ifdef CONFIG_PARPORT
  113. if (pi->pardev)
  114. parport_release((struct pardevice *) (pi->pardev));
  115. #endif
  116. }
  117. void pi_connect(PIA * pi)
  118. {
  119. pi_claim(pi);
  120. pi->proto->connect(pi);
  121. }
  122. EXPORT_SYMBOL(pi_connect);
  123. void pi_disconnect(PIA * pi)
  124. {
  125. pi->proto->disconnect(pi);
  126. pi_unclaim(pi);
  127. }
  128. EXPORT_SYMBOL(pi_disconnect);
  129. static void pi_unregister_parport(PIA * pi)
  130. {
  131. #ifdef CONFIG_PARPORT
  132. if (pi->pardev) {
  133. parport_unregister_device((struct pardevice *) (pi->pardev));
  134. pi->pardev = NULL;
  135. }
  136. #endif
  137. }
  138. void pi_release(PIA * pi)
  139. {
  140. pi_unregister_parport(pi);
  141. #ifndef CONFIG_PARPORT
  142. if (pi->reserved)
  143. release_region(pi->port, pi->reserved);
  144. #endif /* !CONFIG_PARPORT */
  145. if (pi->proto->release_proto)
  146. pi->proto->release_proto(pi);
  147. module_put(pi->proto->owner);
  148. }
  149. EXPORT_SYMBOL(pi_release);
  150. static int default_test_proto(PIA * pi, char *scratch, int verbose)
  151. {
  152. int j, k;
  153. int e[2] = { 0, 0 };
  154. pi->proto->connect(pi);
  155. for (j = 0; j < 2; j++) {
  156. pi_write_regr(pi, 0, 6, 0xa0 + j * 0x10);
  157. for (k = 0; k < 256; k++) {
  158. pi_write_regr(pi, 0, 2, k ^ 0xaa);
  159. pi_write_regr(pi, 0, 3, k ^ 0x55);
  160. if (pi_read_regr(pi, 0, 2) != (k ^ 0xaa))
  161. e[j]++;
  162. }
  163. }
  164. pi->proto->disconnect(pi);
  165. if (verbose)
  166. printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
  167. pi->device, pi->proto->name, pi->port,
  168. pi->mode, e[0], e[1]);
  169. return (e[0] && e[1]); /* not here if both > 0 */
  170. }
  171. static int pi_test_proto(PIA * pi, char *scratch, int verbose)
  172. {
  173. int res;
  174. pi_claim(pi);
  175. if (pi->proto->test_proto)
  176. res = pi->proto->test_proto(pi, scratch, verbose);
  177. else
  178. res = default_test_proto(pi, scratch, verbose);
  179. pi_unclaim(pi);
  180. return res;
  181. }
  182. int pi_register(PIP * pr)
  183. {
  184. int k;
  185. for (k = 0; k < MAX_PROTOS; k++)
  186. if (protocols[k] && !strcmp(pr->name, protocols[k]->name)) {
  187. printk("paride: %s protocol already registered\n",
  188. pr->name);
  189. return 0;
  190. }
  191. k = 0;
  192. while ((k < MAX_PROTOS) && (protocols[k]))
  193. k++;
  194. if (k == MAX_PROTOS) {
  195. printk("paride: protocol table full\n");
  196. return 0;
  197. }
  198. protocols[k] = pr;
  199. pr->index = k;
  200. printk("paride: %s registered as protocol %d\n", pr->name, k);
  201. return 1;
  202. }
  203. EXPORT_SYMBOL(pi_register);
  204. void pi_unregister(PIP * pr)
  205. {
  206. if (!pr)
  207. return;
  208. if (protocols[pr->index] != pr) {
  209. printk("paride: %s not registered\n", pr->name);
  210. return;
  211. }
  212. protocols[pr->index] = NULL;
  213. }
  214. EXPORT_SYMBOL(pi_unregister);
  215. static int pi_register_parport(PIA * pi, int verbose)
  216. {
  217. #ifdef CONFIG_PARPORT
  218. struct parport *port;
  219. port = parport_find_base(pi->port);
  220. if (!port)
  221. return 0;
  222. pi->pardev = parport_register_device(port,
  223. pi->device, NULL,
  224. pi_wake_up, NULL, 0, (void *) pi);
  225. parport_put_port(port);
  226. if (!pi->pardev)
  227. return 0;
  228. init_waitqueue_head(&pi->parq);
  229. if (verbose)
  230. printk("%s: 0x%x is %s\n", pi->device, pi->port, port->name);
  231. pi->parname = (char *) port->name;
  232. #endif
  233. return 1;
  234. }
  235. static int pi_probe_mode(PIA * pi, int max, char *scratch, int verbose)
  236. {
  237. int best, range;
  238. if (pi->mode != -1) {
  239. if (pi->mode >= max)
  240. return 0;
  241. range = 3;
  242. if (pi->mode >= pi->proto->epp_first)
  243. range = 8;
  244. if ((range == 8) && (pi->port % 8))
  245. return 0;
  246. pi->reserved = range;
  247. return (!pi_test_proto(pi, scratch, verbose));
  248. }
  249. best = -1;
  250. for (pi->mode = 0; pi->mode < max; pi->mode++) {
  251. range = 3;
  252. if (pi->mode >= pi->proto->epp_first)
  253. range = 8;
  254. if ((range == 8) && (pi->port % 8))
  255. break;
  256. pi->reserved = range;
  257. if (!pi_test_proto(pi, scratch, verbose))
  258. best = pi->mode;
  259. }
  260. pi->mode = best;
  261. return (best > -1);
  262. }
  263. static int pi_probe_unit(PIA * pi, int unit, char *scratch, int verbose)
  264. {
  265. int max, s, e;
  266. s = unit;
  267. e = s + 1;
  268. if (s == -1) {
  269. s = 0;
  270. e = pi->proto->max_units;
  271. }
  272. if (!pi_register_parport(pi, verbose))
  273. return 0;
  274. if (pi->proto->test_port) {
  275. pi_claim(pi);
  276. max = pi->proto->test_port(pi);
  277. pi_unclaim(pi);
  278. } else
  279. max = pi->proto->max_mode;
  280. if (pi->proto->probe_unit) {
  281. pi_claim(pi);
  282. for (pi->unit = s; pi->unit < e; pi->unit++)
  283. if (pi->proto->probe_unit(pi)) {
  284. pi_unclaim(pi);
  285. if (pi_probe_mode(pi, max, scratch, verbose))
  286. return 1;
  287. pi_unregister_parport(pi);
  288. return 0;
  289. }
  290. pi_unclaim(pi);
  291. pi_unregister_parport(pi);
  292. return 0;
  293. }
  294. if (!pi_probe_mode(pi, max, scratch, verbose)) {
  295. pi_unregister_parport(pi);
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. int pi_init(PIA * pi, int autoprobe, int port, int mode,
  301. int unit, int protocol, int delay, char *scratch,
  302. int devtype, int verbose, char *device)
  303. {
  304. int p, k, s, e;
  305. int lpts[7] = { 0x3bc, 0x378, 0x278, 0x268, 0x27c, 0x26c, 0 };
  306. s = protocol;
  307. e = s + 1;
  308. if (!protocols[0])
  309. request_module("paride_protocol");
  310. if (autoprobe) {
  311. s = 0;
  312. e = MAX_PROTOS;
  313. } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
  314. (!protocols[s]) || (unit < 0) ||
  315. (unit >= protocols[s]->max_units)) {
  316. printk("%s: Invalid parameters\n", device);
  317. return 0;
  318. }
  319. for (p = s; p < e; p++) {
  320. struct pi_protocol *proto = protocols[p];
  321. if (!proto)
  322. continue;
  323. /* still racy */
  324. if (!try_module_get(proto->owner))
  325. continue;
  326. pi->proto = proto;
  327. pi->private = 0;
  328. if (proto->init_proto && proto->init_proto(pi) < 0) {
  329. pi->proto = NULL;
  330. module_put(proto->owner);
  331. continue;
  332. }
  333. if (delay == -1)
  334. pi->delay = pi->proto->default_delay;
  335. else
  336. pi->delay = delay;
  337. pi->devtype = devtype;
  338. pi->device = device;
  339. pi->parname = NULL;
  340. pi->pardev = NULL;
  341. init_waitqueue_head(&pi->parq);
  342. pi->claimed = 0;
  343. pi->claim_cont = NULL;
  344. pi->mode = mode;
  345. if (port != -1) {
  346. pi->port = port;
  347. if (pi_probe_unit(pi, unit, scratch, verbose))
  348. break;
  349. pi->port = 0;
  350. } else {
  351. k = 0;
  352. while ((pi->port = lpts[k++]))
  353. if (pi_probe_unit
  354. (pi, unit, scratch, verbose))
  355. break;
  356. if (pi->port)
  357. break;
  358. }
  359. if (pi->proto->release_proto)
  360. pi->proto->release_proto(pi);
  361. module_put(proto->owner);
  362. }
  363. if (!pi->port) {
  364. if (autoprobe)
  365. printk("%s: Autoprobe failed\n", device);
  366. else
  367. printk("%s: Adapter not found\n", device);
  368. return 0;
  369. }
  370. #ifndef CONFIG_PARPORT
  371. if (!request_region(pi->port, pi->reserved, pi->device)) {
  372. printk(KERN_WARNING "paride: Unable to request region 0x%x\n",
  373. pi->port);
  374. return 0;
  375. }
  376. #endif /* !CONFIG_PARPORT */
  377. if (pi->parname)
  378. printk("%s: Sharing %s at 0x%x\n", pi->device,
  379. pi->parname, pi->port);
  380. pi->proto->log_adapter(pi, scratch, verbose);
  381. return 1;
  382. }
  383. EXPORT_SYMBOL(pi_init);