paride.c 9.1 KB

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