share.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /* $Id: share.c,v 1.1.1.1 2007/06/12 07:27:09 eyryu Exp $
  2. * Parallel-port resource manager code.
  3. *
  4. * Authors: David Campbell <campbell@tirian.che.curtin.edu.au>
  5. * Tim Waugh <tim@cyberelk.demon.co.uk>
  6. * Jose Renau <renau@acm.org>
  7. * Philip Blundell <philb@gnu.org>
  8. * Andrea Arcangeli
  9. *
  10. * based on work by Grant Guenther <grant@torque.net>
  11. * and Philip Blundell
  12. *
  13. * Any part of this program may be used in documents licensed under
  14. * the GNU Free Documentation License, Version 1.1 or any later version
  15. * published by the Free Software Foundation.
  16. */
  17. #undef PARPORT_DEBUG_SHARING /* undef for production */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/threads.h>
  21. #include <linux/parport.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/ioport.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/kmod.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mutex.h>
  32. #include <asm/irq.h>
  33. #undef PARPORT_PARANOID
  34. #define PARPORT_DEFAULT_TIMESLICE (HZ/5)
  35. unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
  36. int parport_default_spintime = DEFAULT_SPIN_TIME;
  37. static LIST_HEAD(portlist);
  38. static DEFINE_SPINLOCK(parportlist_lock);
  39. /* list of all allocated ports, sorted by ->number */
  40. static LIST_HEAD(all_ports);
  41. static DEFINE_SPINLOCK(full_list_lock);
  42. static LIST_HEAD(drivers);
  43. static DEFINE_MUTEX(registration_lock);
  44. /* What you can do to a port that's gone away.. */
  45. static void dead_write_lines (struct parport *p, unsigned char b){}
  46. static unsigned char dead_read_lines (struct parport *p) { return 0; }
  47. static unsigned char dead_frob_lines (struct parport *p, unsigned char b,
  48. unsigned char c) { return 0; }
  49. static void dead_onearg (struct parport *p){}
  50. static void dead_initstate (struct pardevice *d, struct parport_state *s) { }
  51. static void dead_state (struct parport *p, struct parport_state *s) { }
  52. static size_t dead_write (struct parport *p, const void *b, size_t l, int f)
  53. { return 0; }
  54. static size_t dead_read (struct parport *p, void *b, size_t l, int f)
  55. { return 0; }
  56. static struct parport_operations dead_ops = {
  57. .write_data = dead_write_lines, /* data */
  58. .read_data = dead_read_lines,
  59. .write_control = dead_write_lines, /* control */
  60. .read_control = dead_read_lines,
  61. .frob_control = dead_frob_lines,
  62. .read_status = dead_read_lines, /* status */
  63. .enable_irq = dead_onearg, /* enable_irq */
  64. .disable_irq = dead_onearg, /* disable_irq */
  65. .data_forward = dead_onearg, /* data_forward */
  66. .data_reverse = dead_onearg, /* data_reverse */
  67. .init_state = dead_initstate, /* init_state */
  68. .save_state = dead_state,
  69. .restore_state = dead_state,
  70. .epp_write_data = dead_write, /* epp */
  71. .epp_read_data = dead_read,
  72. .epp_write_addr = dead_write,
  73. .epp_read_addr = dead_read,
  74. .ecp_write_data = dead_write, /* ecp */
  75. .ecp_read_data = dead_read,
  76. .ecp_write_addr = dead_write,
  77. .compat_write_data = dead_write, /* compat */
  78. .nibble_read_data = dead_read, /* nibble */
  79. .byte_read_data = dead_read, /* byte */
  80. .owner = NULL,
  81. };
  82. /* Call attach(port) for each registered driver. */
  83. static void attach_driver_chain(struct parport *port)
  84. {
  85. /* caller has exclusive registration_lock */
  86. struct parport_driver *drv;
  87. list_for_each_entry(drv, &drivers, list)
  88. drv->attach(port);
  89. }
  90. /* Call detach(port) for each registered driver. */
  91. static void detach_driver_chain(struct parport *port)
  92. {
  93. struct parport_driver *drv;
  94. /* caller has exclusive registration_lock */
  95. list_for_each_entry(drv, &drivers, list)
  96. drv->detach (port);
  97. }
  98. /* Ask kmod for some lowlevel drivers. */
  99. static void get_lowlevel_driver (void)
  100. {
  101. /* There is no actual module called this: you should set
  102. * up an alias for modutils. */
  103. request_module ("parport_lowlevel");
  104. }
  105. /**
  106. * parport_register_driver - register a parallel port device driver
  107. * @drv: structure describing the driver
  108. *
  109. * This can be called by a parallel port device driver in order
  110. * to receive notifications about ports being found in the
  111. * system, as well as ports no longer available.
  112. *
  113. * The @drv structure is allocated by the caller and must not be
  114. * deallocated until after calling parport_unregister_driver().
  115. *
  116. * The driver's attach() function may block. The port that
  117. * attach() is given will be valid for the duration of the
  118. * callback, but if the driver wants to take a copy of the
  119. * pointer it must call parport_get_port() to do so. Calling
  120. * parport_register_device() on that port will do this for you.
  121. *
  122. * The driver's detach() function may block. The port that
  123. * detach() is given will be valid for the duration of the
  124. * callback, but if the driver wants to take a copy of the
  125. * pointer it must call parport_get_port() to do so.
  126. *
  127. * Returns 0 on success. Currently it always succeeds.
  128. **/
  129. int parport_register_driver (struct parport_driver *drv)
  130. {
  131. struct parport *port;
  132. if (list_empty(&portlist))
  133. get_lowlevel_driver ();
  134. mutex_lock(&registration_lock);
  135. list_for_each_entry(port, &portlist, list)
  136. drv->attach(port);
  137. list_add(&drv->list, &drivers);
  138. mutex_unlock(&registration_lock);
  139. return 0;
  140. }
  141. /**
  142. * parport_unregister_driver - deregister a parallel port device driver
  143. * @drv: structure describing the driver that was given to
  144. * parport_register_driver()
  145. *
  146. * This should be called by a parallel port device driver that
  147. * has registered itself using parport_register_driver() when it
  148. * is about to be unloaded.
  149. *
  150. * When it returns, the driver's attach() routine will no longer
  151. * be called, and for each port that attach() was called for, the
  152. * detach() routine will have been called.
  153. *
  154. * All the driver's attach() and detach() calls are guaranteed to have
  155. * finished by the time this function returns.
  156. **/
  157. void parport_unregister_driver (struct parport_driver *drv)
  158. {
  159. struct parport *port;
  160. mutex_lock(&registration_lock);
  161. list_del_init(&drv->list);
  162. list_for_each_entry(port, &portlist, list)
  163. drv->detach(port);
  164. mutex_unlock(&registration_lock);
  165. }
  166. static void free_port (struct parport *port)
  167. {
  168. int d;
  169. spin_lock(&full_list_lock);
  170. list_del(&port->full_list);
  171. spin_unlock(&full_list_lock);
  172. for (d = 0; d < 5; d++) {
  173. kfree(port->probe_info[d].class_name);
  174. kfree(port->probe_info[d].mfr);
  175. kfree(port->probe_info[d].model);
  176. kfree(port->probe_info[d].cmdset);
  177. kfree(port->probe_info[d].description);
  178. }
  179. kfree(port->name);
  180. kfree(port);
  181. }
  182. /**
  183. * parport_get_port - increment a port's reference count
  184. * @port: the port
  185. *
  186. * This ensures that a struct parport pointer remains valid
  187. * until the matching parport_put_port() call.
  188. **/
  189. struct parport *parport_get_port (struct parport *port)
  190. {
  191. atomic_inc (&port->ref_count);
  192. return port;
  193. }
  194. /**
  195. * parport_put_port - decrement a port's reference count
  196. * @port: the port
  197. *
  198. * This should be called once for each call to parport_get_port(),
  199. * once the port is no longer needed.
  200. **/
  201. void parport_put_port (struct parport *port)
  202. {
  203. if (atomic_dec_and_test (&port->ref_count))
  204. /* Can destroy it now. */
  205. free_port (port);
  206. return;
  207. }
  208. /**
  209. * parport_register_port - register a parallel port
  210. * @base: base I/O address
  211. * @irq: IRQ line
  212. * @dma: DMA channel
  213. * @ops: pointer to the port driver's port operations structure
  214. *
  215. * When a parallel port (lowlevel) driver finds a port that
  216. * should be made available to parallel port device drivers, it
  217. * should call parport_register_port(). The @base, @irq, and
  218. * @dma parameters are for the convenience of port drivers, and
  219. * for ports where they aren't meaningful needn't be set to
  220. * anything special. They can be altered afterwards by adjusting
  221. * the relevant members of the parport structure that is returned
  222. * and represents the port. They should not be tampered with
  223. * after calling parport_announce_port, however.
  224. *
  225. * If there are parallel port device drivers in the system that
  226. * have registered themselves using parport_register_driver(),
  227. * they are not told about the port at this time; that is done by
  228. * parport_announce_port().
  229. *
  230. * The @ops structure is allocated by the caller, and must not be
  231. * deallocated before calling parport_remove_port().
  232. *
  233. * If there is no memory to allocate a new parport structure,
  234. * this function will return %NULL.
  235. **/
  236. struct parport *parport_register_port(unsigned long base, int irq, int dma,
  237. struct parport_operations *ops)
  238. {
  239. struct list_head *l;
  240. struct parport *tmp;
  241. int num;
  242. int device;
  243. char *name;
  244. tmp = kmalloc(sizeof(struct parport), GFP_KERNEL);
  245. if (!tmp) {
  246. printk(KERN_WARNING "parport: memory squeeze\n");
  247. return NULL;
  248. }
  249. /* Init our structure */
  250. memset(tmp, 0, sizeof(struct parport));
  251. tmp->base = base;
  252. tmp->irq = irq;
  253. tmp->dma = dma;
  254. tmp->muxport = tmp->daisy = tmp->muxsel = -1;
  255. tmp->modes = 0;
  256. INIT_LIST_HEAD(&tmp->list);
  257. tmp->devices = tmp->cad = NULL;
  258. tmp->flags = 0;
  259. tmp->ops = ops;
  260. tmp->physport = tmp;
  261. memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info));
  262. rwlock_init(&tmp->cad_lock);
  263. spin_lock_init(&tmp->waitlist_lock);
  264. spin_lock_init(&tmp->pardevice_lock);
  265. tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
  266. tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
  267. init_MUTEX_LOCKED (&tmp->ieee1284.irq); /* actually a semaphore at 0 */
  268. tmp->spintime = parport_default_spintime;
  269. atomic_set (&tmp->ref_count, 1);
  270. INIT_LIST_HEAD(&tmp->full_list);
  271. name = kmalloc(15, GFP_KERNEL);
  272. if (!name) {
  273. printk(KERN_ERR "parport: memory squeeze\n");
  274. kfree(tmp);
  275. return NULL;
  276. }
  277. /* Search for the lowest free parport number. */
  278. spin_lock(&full_list_lock);
  279. for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
  280. struct parport *p = list_entry(l, struct parport, full_list);
  281. if (p->number != num)
  282. break;
  283. }
  284. tmp->portnum = tmp->number = num;
  285. list_add_tail(&tmp->full_list, l);
  286. spin_unlock(&full_list_lock);
  287. /*
  288. * Now that the portnum is known finish doing the Init.
  289. */
  290. sprintf(name, "parport%d", tmp->portnum = tmp->number);
  291. tmp->name = name;
  292. for (device = 0; device < 5; device++)
  293. /* assume the worst */
  294. tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
  295. tmp->waithead = tmp->waittail = NULL;
  296. return tmp;
  297. }
  298. /**
  299. * parport_announce_port - tell device drivers about a parallel port
  300. * @port: parallel port to announce
  301. *
  302. * After a port driver has registered a parallel port with
  303. * parport_register_port, and performed any necessary
  304. * initialisation or adjustments, it should call
  305. * parport_announce_port() in order to notify all device drivers
  306. * that have called parport_register_driver(). Their attach()
  307. * functions will be called, with @port as the parameter.
  308. **/
  309. void parport_announce_port (struct parport *port)
  310. {
  311. int i;
  312. #ifdef CONFIG_PARPORT_1284
  313. /* Analyse the IEEE1284.3 topology of the port. */
  314. parport_daisy_init(port);
  315. #endif
  316. parport_proc_register(port);
  317. mutex_lock(&registration_lock);
  318. spin_lock_irq(&parportlist_lock);
  319. list_add_tail(&port->list, &portlist);
  320. for (i = 1; i < 3; i++) {
  321. struct parport *slave = port->slaves[i-1];
  322. if (slave)
  323. list_add_tail(&slave->list, &portlist);
  324. }
  325. spin_unlock_irq(&parportlist_lock);
  326. /* Let drivers know that new port(s) has arrived. */
  327. attach_driver_chain (port);
  328. for (i = 1; i < 3; i++) {
  329. struct parport *slave = port->slaves[i-1];
  330. if (slave)
  331. attach_driver_chain(slave);
  332. }
  333. mutex_unlock(&registration_lock);
  334. }
  335. /**
  336. * parport_remove_port - deregister a parallel port
  337. * @port: parallel port to deregister
  338. *
  339. * When a parallel port driver is forcibly unloaded, or a
  340. * parallel port becomes inaccessible, the port driver must call
  341. * this function in order to deal with device drivers that still
  342. * want to use it.
  343. *
  344. * The parport structure associated with the port has its
  345. * operations structure replaced with one containing 'null'
  346. * operations that return errors or just don't do anything.
  347. *
  348. * Any drivers that have registered themselves using
  349. * parport_register_driver() are notified that the port is no
  350. * longer accessible by having their detach() routines called
  351. * with @port as the parameter.
  352. **/
  353. void parport_remove_port(struct parport *port)
  354. {
  355. int i;
  356. mutex_lock(&registration_lock);
  357. /* Spread the word. */
  358. detach_driver_chain (port);
  359. #ifdef CONFIG_PARPORT_1284
  360. /* Forget the IEEE1284.3 topology of the port. */
  361. parport_daisy_fini(port);
  362. for (i = 1; i < 3; i++) {
  363. struct parport *slave = port->slaves[i-1];
  364. if (!slave)
  365. continue;
  366. detach_driver_chain(slave);
  367. parport_daisy_fini(slave);
  368. }
  369. #endif
  370. port->ops = &dead_ops;
  371. spin_lock(&parportlist_lock);
  372. list_del_init(&port->list);
  373. for (i = 1; i < 3; i++) {
  374. struct parport *slave = port->slaves[i-1];
  375. if (slave)
  376. list_del_init(&slave->list);
  377. }
  378. spin_unlock(&parportlist_lock);
  379. mutex_unlock(&registration_lock);
  380. parport_proc_unregister(port);
  381. for (i = 1; i < 3; i++) {
  382. struct parport *slave = port->slaves[i-1];
  383. if (slave)
  384. parport_put_port(slave);
  385. }
  386. }
  387. /**
  388. * parport_register_device - register a device on a parallel port
  389. * @port: port to which the device is attached
  390. * @name: a name to refer to the device
  391. * @pf: preemption callback
  392. * @kf: kick callback (wake-up)
  393. * @irq_func: interrupt handler
  394. * @flags: registration flags
  395. * @handle: data for callback functions
  396. *
  397. * This function, called by parallel port device drivers,
  398. * declares that a device is connected to a port, and tells the
  399. * system all it needs to know.
  400. *
  401. * The @name is allocated by the caller and must not be
  402. * deallocated until the caller calls @parport_unregister_device
  403. * for that device.
  404. *
  405. * The preemption callback function, @pf, is called when this
  406. * device driver has claimed access to the port but another
  407. * device driver wants to use it. It is given @handle as its
  408. * parameter, and should return zero if it is willing for the
  409. * system to release the port to another driver on its behalf.
  410. * If it wants to keep control of the port it should return
  411. * non-zero, and no action will be taken. It is good manners for
  412. * the driver to try to release the port at the earliest
  413. * opportunity after its preemption callback rejects a preemption
  414. * attempt. Note that if a preemption callback is happy for
  415. * preemption to go ahead, there is no need to release the port;
  416. * it is done automatically. This function may not block, as it
  417. * may be called from interrupt context. If the device driver
  418. * does not support preemption, @pf can be %NULL.
  419. *
  420. * The wake-up ("kick") callback function, @kf, is called when
  421. * the port is available to be claimed for exclusive access; that
  422. * is, parport_claim() is guaranteed to succeed when called from
  423. * inside the wake-up callback function. If the driver wants to
  424. * claim the port it should do so; otherwise, it need not take
  425. * any action. This function may not block, as it may be called
  426. * from interrupt context. If the device driver does not want to
  427. * be explicitly invited to claim the port in this way, @kf can
  428. * be %NULL.
  429. *
  430. * The interrupt handler, @irq_func, is called when an interrupt
  431. * arrives from the parallel port. Note that if a device driver
  432. * wants to use interrupts it should use parport_enable_irq(),
  433. * and can also check the irq member of the parport structure
  434. * representing the port.
  435. *
  436. * The parallel port (lowlevel) driver is the one that has called
  437. * request_irq() and whose interrupt handler is called first.
  438. * This handler does whatever needs to be done to the hardware to
  439. * acknowledge the interrupt (for PC-style ports there is nothing
  440. * special to be done). It then tells the IEEE 1284 code about
  441. * the interrupt, which may involve reacting to an IEEE 1284
  442. * event depending on the current IEEE 1284 phase. After this,
  443. * it calls @irq_func. Needless to say, @irq_func will be called
  444. * from interrupt context, and may not block.
  445. *
  446. * The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
  447. * so should only be used when sharing the port with other device
  448. * drivers is impossible and would lead to incorrect behaviour.
  449. * Use it sparingly! Normally, @flags will be zero.
  450. *
  451. * This function returns a pointer to a structure that represents
  452. * the device on the port, or %NULL if there is not enough memory
  453. * to allocate space for that structure.
  454. **/
  455. struct pardevice *
  456. parport_register_device(struct parport *port, const char *name,
  457. int (*pf)(void *), void (*kf)(void *),
  458. void (*irq_func)(int, void *),
  459. int flags, void *handle)
  460. {
  461. struct pardevice *tmp;
  462. if (port->physport->flags & PARPORT_FLAG_EXCL) {
  463. /* An exclusive device is registered. */
  464. printk (KERN_DEBUG "%s: no more devices allowed\n",
  465. port->name);
  466. return NULL;
  467. }
  468. if (flags & PARPORT_DEV_LURK) {
  469. if (!pf || !kf) {
  470. printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
  471. return NULL;
  472. }
  473. }
  474. /* We up our own module reference count, and that of the port
  475. on which a device is to be registered, to ensure that
  476. neither of us gets unloaded while we sleep in (e.g.)
  477. kmalloc.
  478. */
  479. if (!try_module_get(port->ops->owner)) {
  480. return NULL;
  481. }
  482. parport_get_port (port);
  483. tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
  484. if (tmp == NULL) {
  485. printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
  486. goto out;
  487. }
  488. tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
  489. if (tmp->state == NULL) {
  490. printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
  491. goto out_free_pardevice;
  492. }
  493. tmp->name = name;
  494. tmp->port = port;
  495. tmp->daisy = -1;
  496. tmp->preempt = pf;
  497. tmp->wakeup = kf;
  498. tmp->private = handle;
  499. tmp->flags = flags;
  500. tmp->irq_func = irq_func;
  501. tmp->waiting = 0;
  502. tmp->timeout = 5 * HZ;
  503. /* Chain this onto the list */
  504. tmp->prev = NULL;
  505. /*
  506. * This function must not run from an irq handler so we don' t need
  507. * to clear irq on the local CPU. -arca
  508. */
  509. spin_lock(&port->physport->pardevice_lock);
  510. if (flags & PARPORT_DEV_EXCL) {
  511. if (port->physport->devices) {
  512. spin_unlock (&port->physport->pardevice_lock);
  513. printk (KERN_DEBUG
  514. "%s: cannot grant exclusive access for "
  515. "device %s\n", port->name, name);
  516. goto out_free_all;
  517. }
  518. port->flags |= PARPORT_FLAG_EXCL;
  519. }
  520. tmp->next = port->physport->devices;
  521. wmb(); /* Make sure that tmp->next is written before it's
  522. added to the list; see comments marked 'no locking
  523. required' */
  524. if (port->physport->devices)
  525. port->physport->devices->prev = tmp;
  526. port->physport->devices = tmp;
  527. spin_unlock(&port->physport->pardevice_lock);
  528. init_waitqueue_head(&tmp->wait_q);
  529. tmp->timeslice = parport_default_timeslice;
  530. tmp->waitnext = tmp->waitprev = NULL;
  531. /*
  532. * This has to be run as last thing since init_state may need other
  533. * pardevice fields. -arca
  534. */
  535. port->ops->init_state(tmp, tmp->state);
  536. parport_device_proc_register(tmp);
  537. return tmp;
  538. out_free_all:
  539. kfree(tmp->state);
  540. out_free_pardevice:
  541. kfree(tmp);
  542. out:
  543. parport_put_port (port);
  544. module_put(port->ops->owner);
  545. return NULL;
  546. }
  547. /**
  548. * parport_unregister_device - deregister a device on a parallel port
  549. * @dev: pointer to structure representing device
  550. *
  551. * This undoes the effect of parport_register_device().
  552. **/
  553. void parport_unregister_device(struct pardevice *dev)
  554. {
  555. struct parport *port;
  556. #ifdef PARPORT_PARANOID
  557. if (dev == NULL) {
  558. printk(KERN_ERR "parport_unregister_device: passed NULL\n");
  559. return;
  560. }
  561. #endif
  562. parport_device_proc_unregister(dev);
  563. port = dev->port->physport;
  564. if (port->cad == dev) {
  565. printk(KERN_DEBUG "%s: %s forgot to release port\n",
  566. port->name, dev->name);
  567. parport_release (dev);
  568. }
  569. spin_lock(&port->pardevice_lock);
  570. if (dev->next)
  571. dev->next->prev = dev->prev;
  572. if (dev->prev)
  573. dev->prev->next = dev->next;
  574. else
  575. port->devices = dev->next;
  576. if (dev->flags & PARPORT_DEV_EXCL)
  577. port->flags &= ~PARPORT_FLAG_EXCL;
  578. spin_unlock(&port->pardevice_lock);
  579. /* Make sure we haven't left any pointers around in the wait
  580. * list. */
  581. spin_lock (&port->waitlist_lock);
  582. if (dev->waitprev || dev->waitnext || port->waithead == dev) {
  583. if (dev->waitprev)
  584. dev->waitprev->waitnext = dev->waitnext;
  585. else
  586. port->waithead = dev->waitnext;
  587. if (dev->waitnext)
  588. dev->waitnext->waitprev = dev->waitprev;
  589. else
  590. port->waittail = dev->waitprev;
  591. }
  592. spin_unlock (&port->waitlist_lock);
  593. kfree(dev->state);
  594. kfree(dev);
  595. module_put(port->ops->owner);
  596. parport_put_port (port);
  597. }
  598. /**
  599. * parport_find_number - find a parallel port by number
  600. * @number: parallel port number
  601. *
  602. * This returns the parallel port with the specified number, or
  603. * %NULL if there is none.
  604. *
  605. * There is an implicit parport_get_port() done already; to throw
  606. * away the reference to the port that parport_find_number()
  607. * gives you, use parport_put_port().
  608. */
  609. struct parport *parport_find_number (int number)
  610. {
  611. struct parport *port, *result = NULL;
  612. if (list_empty(&portlist))
  613. get_lowlevel_driver ();
  614. spin_lock (&parportlist_lock);
  615. list_for_each_entry(port, &portlist, list) {
  616. if (port->number == number) {
  617. result = parport_get_port (port);
  618. break;
  619. }
  620. }
  621. spin_unlock (&parportlist_lock);
  622. return result;
  623. }
  624. /**
  625. * parport_find_base - find a parallel port by base address
  626. * @base: base I/O address
  627. *
  628. * This returns the parallel port with the specified base
  629. * address, or %NULL if there is none.
  630. *
  631. * There is an implicit parport_get_port() done already; to throw
  632. * away the reference to the port that parport_find_base()
  633. * gives you, use parport_put_port().
  634. */
  635. struct parport *parport_find_base (unsigned long base)
  636. {
  637. struct parport *port, *result = NULL;
  638. if (list_empty(&portlist))
  639. get_lowlevel_driver ();
  640. spin_lock (&parportlist_lock);
  641. list_for_each_entry(port, &portlist, list) {
  642. if (port->base == base) {
  643. result = parport_get_port (port);
  644. break;
  645. }
  646. }
  647. spin_unlock (&parportlist_lock);
  648. return result;
  649. }
  650. /**
  651. * parport_claim - claim access to a parallel port device
  652. * @dev: pointer to structure representing a device on the port
  653. *
  654. * This function will not block and so can be used from interrupt
  655. * context. If parport_claim() succeeds in claiming access to
  656. * the port it returns zero and the port is available to use. It
  657. * may fail (returning non-zero) if the port is in use by another
  658. * driver and that driver is not willing to relinquish control of
  659. * the port.
  660. **/
  661. int parport_claim(struct pardevice *dev)
  662. {
  663. struct pardevice *oldcad;
  664. struct parport *port = dev->port->physport;
  665. unsigned long flags;
  666. if (port->cad == dev) {
  667. printk(KERN_INFO "%s: %s already owner\n",
  668. dev->port->name,dev->name);
  669. return 0;
  670. }
  671. /* Preempt any current device */
  672. write_lock_irqsave (&port->cad_lock, flags);
  673. if ((oldcad = port->cad) != NULL) {
  674. if (oldcad->preempt) {
  675. if (oldcad->preempt(oldcad->private))
  676. goto blocked;
  677. port->ops->save_state(port, dev->state);
  678. } else
  679. goto blocked;
  680. if (port->cad != oldcad) {
  681. /* I think we'll actually deadlock rather than
  682. get here, but just in case.. */
  683. printk(KERN_WARNING
  684. "%s: %s released port when preempted!\n",
  685. port->name, oldcad->name);
  686. if (port->cad)
  687. goto blocked;
  688. }
  689. }
  690. /* Can't fail from now on, so mark ourselves as no longer waiting. */
  691. if (dev->waiting & 1) {
  692. dev->waiting = 0;
  693. /* Take ourselves out of the wait list again. */
  694. spin_lock_irq (&port->waitlist_lock);
  695. if (dev->waitprev)
  696. dev->waitprev->waitnext = dev->waitnext;
  697. else
  698. port->waithead = dev->waitnext;
  699. if (dev->waitnext)
  700. dev->waitnext->waitprev = dev->waitprev;
  701. else
  702. port->waittail = dev->waitprev;
  703. spin_unlock_irq (&port->waitlist_lock);
  704. dev->waitprev = dev->waitnext = NULL;
  705. }
  706. /* Now we do the change of devices */
  707. port->cad = dev;
  708. #ifdef CONFIG_PARPORT_1284
  709. /* If it's a mux port, select it. */
  710. if (dev->port->muxport >= 0) {
  711. /* FIXME */
  712. port->muxsel = dev->port->muxport;
  713. }
  714. /* If it's a daisy chain device, select it. */
  715. if (dev->daisy >= 0) {
  716. /* This could be lazier. */
  717. if (!parport_daisy_select (port, dev->daisy,
  718. IEEE1284_MODE_COMPAT))
  719. port->daisy = dev->daisy;
  720. }
  721. #endif /* IEEE1284.3 support */
  722. /* Restore control registers */
  723. port->ops->restore_state(port, dev->state);
  724. write_unlock_irqrestore(&port->cad_lock, flags);
  725. dev->time = jiffies;
  726. return 0;
  727. blocked:
  728. /* If this is the first time we tried to claim the port, register an
  729. interest. This is only allowed for devices sleeping in
  730. parport_claim_or_block(), or those with a wakeup function. */
  731. /* The cad_lock is still held for writing here */
  732. if (dev->waiting & 2 || dev->wakeup) {
  733. spin_lock (&port->waitlist_lock);
  734. if (test_and_set_bit(0, &dev->waiting) == 0) {
  735. /* First add ourselves to the end of the wait list. */
  736. dev->waitnext = NULL;
  737. dev->waitprev = port->waittail;
  738. if (port->waittail) {
  739. port->waittail->waitnext = dev;
  740. port->waittail = dev;
  741. } else
  742. port->waithead = port->waittail = dev;
  743. }
  744. spin_unlock (&port->waitlist_lock);
  745. }
  746. write_unlock_irqrestore (&port->cad_lock, flags);
  747. return -EAGAIN;
  748. }
  749. /**
  750. * parport_claim_or_block - claim access to a parallel port device
  751. * @dev: pointer to structure representing a device on the port
  752. *
  753. * This behaves like parport_claim(), but will block if necessary
  754. * to wait for the port to be free. A return value of 1
  755. * indicates that it slept; 0 means that it succeeded without
  756. * needing to sleep. A negative error code indicates failure.
  757. **/
  758. int parport_claim_or_block(struct pardevice *dev)
  759. {
  760. int r;
  761. /* Signal to parport_claim() that we can wait even without a
  762. wakeup function. */
  763. dev->waiting = 2;
  764. /* Try to claim the port. If this fails, we need to sleep. */
  765. r = parport_claim(dev);
  766. if (r == -EAGAIN) {
  767. #ifdef PARPORT_DEBUG_SHARING
  768. printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
  769. #endif
  770. /*
  771. * FIXME!!! Use the proper locking for dev->waiting,
  772. * and make this use the "wait_event_interruptible()"
  773. * interfaces. The cli/sti that used to be here
  774. * did nothing.
  775. *
  776. * See also parport_release()
  777. */
  778. /* If dev->waiting is clear now, an interrupt
  779. gave us the port and we would deadlock if we slept. */
  780. if (dev->waiting) {
  781. interruptible_sleep_on (&dev->wait_q);
  782. if (signal_pending (current)) {
  783. return -EINTR;
  784. }
  785. r = 1;
  786. } else {
  787. r = 0;
  788. #ifdef PARPORT_DEBUG_SHARING
  789. printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
  790. dev->name);
  791. #endif
  792. }
  793. #ifdef PARPORT_DEBUG_SHARING
  794. if (dev->port->physport->cad != dev)
  795. printk(KERN_DEBUG "%s: exiting parport_claim_or_block "
  796. "but %s owns port!\n", dev->name,
  797. dev->port->physport->cad ?
  798. dev->port->physport->cad->name:"nobody");
  799. #endif
  800. }
  801. dev->waiting = 0;
  802. return r;
  803. }
  804. /**
  805. * parport_release - give up access to a parallel port device
  806. * @dev: pointer to structure representing parallel port device
  807. *
  808. * This function cannot fail, but it should not be called without
  809. * the port claimed. Similarly, if the port is already claimed
  810. * you should not try claiming it again.
  811. **/
  812. void parport_release(struct pardevice *dev)
  813. {
  814. struct parport *port = dev->port->physport;
  815. struct pardevice *pd;
  816. unsigned long flags;
  817. /* Make sure that dev is the current device */
  818. write_lock_irqsave(&port->cad_lock, flags);
  819. if (port->cad != dev) {
  820. write_unlock_irqrestore (&port->cad_lock, flags);
  821. printk(KERN_WARNING "%s: %s tried to release parport "
  822. "when not owner\n", port->name, dev->name);
  823. return;
  824. }
  825. #ifdef CONFIG_PARPORT_1284
  826. /* If this is on a mux port, deselect it. */
  827. if (dev->port->muxport >= 0) {
  828. /* FIXME */
  829. port->muxsel = -1;
  830. }
  831. /* If this is a daisy device, deselect it. */
  832. if (dev->daisy >= 0) {
  833. parport_daisy_deselect_all (port);
  834. port->daisy = -1;
  835. }
  836. #endif
  837. port->cad = NULL;
  838. write_unlock_irqrestore(&port->cad_lock, flags);
  839. /* Save control registers */
  840. port->ops->save_state(port, dev->state);
  841. /* If anybody is waiting, find out who's been there longest and
  842. then wake them up. (Note: no locking required) */
  843. /* !!! LOCKING IS NEEDED HERE */
  844. for (pd = port->waithead; pd; pd = pd->waitnext) {
  845. if (pd->waiting & 2) { /* sleeping in claim_or_block */
  846. parport_claim(pd);
  847. if (waitqueue_active(&pd->wait_q))
  848. wake_up_interruptible(&pd->wait_q);
  849. return;
  850. } else if (pd->wakeup) {
  851. pd->wakeup(pd->private);
  852. if (dev->port->cad) /* racy but no matter */
  853. return;
  854. } else {
  855. printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
  856. }
  857. }
  858. /* Nobody was waiting, so walk the list to see if anyone is
  859. interested in being woken up. (Note: no locking required) */
  860. /* !!! LOCKING IS NEEDED HERE */
  861. for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) {
  862. if (pd->wakeup && pd != dev)
  863. pd->wakeup(pd->private);
  864. }
  865. }
  866. /* Exported symbols for modules. */
  867. EXPORT_SYMBOL(parport_claim);
  868. EXPORT_SYMBOL(parport_claim_or_block);
  869. EXPORT_SYMBOL(parport_release);
  870. EXPORT_SYMBOL(parport_register_port);
  871. EXPORT_SYMBOL(parport_announce_port);
  872. EXPORT_SYMBOL(parport_remove_port);
  873. EXPORT_SYMBOL(parport_register_driver);
  874. EXPORT_SYMBOL(parport_unregister_driver);
  875. EXPORT_SYMBOL(parport_register_device);
  876. EXPORT_SYMBOL(parport_unregister_device);
  877. EXPORT_SYMBOL(parport_get_port);
  878. EXPORT_SYMBOL(parport_put_port);
  879. EXPORT_SYMBOL(parport_find_number);
  880. EXPORT_SYMBOL(parport_find_base);
  881. MODULE_LICENSE("GPL");