paride.c 9.4 KB

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