serio.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * The Serio abstraction module
  3. *
  4. * Copyright (c) 1999-2004 Vojtech Pavlik
  5. * Copyright (c) 2004 Dmitry Torokhov
  6. * Copyright (c) 2003 Daniele Bellucci
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Should you need to contact me, the author, you can do so either by
  24. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  25. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  26. */
  27. #include <linux/stddef.h>
  28. #include <linux/module.h>
  29. #include <linux/serio.h>
  30. #include <linux/errno.h>
  31. #include <linux/wait.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/kthread.h>
  35. #include <linux/mutex.h>
  36. #include <linux/freezer.h>
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION("Serio abstraction core");
  39. MODULE_LICENSE("GPL");
  40. EXPORT_SYMBOL(serio_interrupt);
  41. EXPORT_SYMBOL(__serio_register_port);
  42. EXPORT_SYMBOL(serio_unregister_port);
  43. EXPORT_SYMBOL(serio_unregister_child_port);
  44. EXPORT_SYMBOL(__serio_register_driver);
  45. EXPORT_SYMBOL(serio_unregister_driver);
  46. EXPORT_SYMBOL(serio_open);
  47. EXPORT_SYMBOL(serio_close);
  48. EXPORT_SYMBOL(serio_rescan);
  49. EXPORT_SYMBOL(serio_reconnect);
  50. /*
  51. * serio_mutex protects entire serio subsystem and is taken every time
  52. * serio port or driver registrered or unregistered.
  53. */
  54. static DEFINE_MUTEX(serio_mutex);
  55. static LIST_HEAD(serio_list);
  56. static struct bus_type serio_bus;
  57. static void serio_add_port(struct serio *serio);
  58. static void serio_reconnect_port(struct serio *serio);
  59. static void serio_disconnect_port(struct serio *serio);
  60. static void serio_attach_driver(struct serio_driver *drv);
  61. static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
  62. {
  63. int retval;
  64. mutex_lock(&serio->drv_mutex);
  65. retval = drv->connect(serio, drv);
  66. mutex_unlock(&serio->drv_mutex);
  67. return retval;
  68. }
  69. static int serio_reconnect_driver(struct serio *serio)
  70. {
  71. int retval = -1;
  72. mutex_lock(&serio->drv_mutex);
  73. if (serio->drv && serio->drv->reconnect)
  74. retval = serio->drv->reconnect(serio);
  75. mutex_unlock(&serio->drv_mutex);
  76. return retval;
  77. }
  78. static void serio_disconnect_driver(struct serio *serio)
  79. {
  80. mutex_lock(&serio->drv_mutex);
  81. if (serio->drv)
  82. serio->drv->disconnect(serio);
  83. mutex_unlock(&serio->drv_mutex);
  84. }
  85. static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
  86. {
  87. while (ids->type || ids->proto) {
  88. if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
  89. (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
  90. (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
  91. (ids->id == SERIO_ANY || ids->id == serio->id.id))
  92. return 1;
  93. ids++;
  94. }
  95. return 0;
  96. }
  97. /*
  98. * Basic serio -> driver core mappings
  99. */
  100. static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
  101. {
  102. int error;
  103. if (serio_match_port(drv->id_table, serio)) {
  104. serio->dev.driver = &drv->driver;
  105. if (serio_connect_driver(serio, drv)) {
  106. serio->dev.driver = NULL;
  107. return -ENODEV;
  108. }
  109. error = device_bind_driver(&serio->dev);
  110. if (error) {
  111. printk(KERN_WARNING
  112. "serio: device_bind_driver() failed "
  113. "for %s (%s) and %s, error: %d\n",
  114. serio->phys, serio->name,
  115. drv->description, error);
  116. serio_disconnect_driver(serio);
  117. serio->dev.driver = NULL;
  118. return error;
  119. }
  120. }
  121. return 0;
  122. }
  123. static void serio_find_driver(struct serio *serio)
  124. {
  125. int error;
  126. error = device_attach(&serio->dev);
  127. if (error < 0)
  128. printk(KERN_WARNING
  129. "serio: device_attach() failed for %s (%s), error: %d\n",
  130. serio->phys, serio->name, error);
  131. }
  132. /*
  133. * Serio event processing.
  134. */
  135. enum serio_event_type {
  136. SERIO_RESCAN_PORT,
  137. SERIO_RECONNECT_PORT,
  138. SERIO_REGISTER_PORT,
  139. SERIO_ATTACH_DRIVER,
  140. };
  141. struct serio_event {
  142. enum serio_event_type type;
  143. void *object;
  144. struct module *owner;
  145. struct list_head node;
  146. };
  147. static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
  148. static LIST_HEAD(serio_event_list);
  149. static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
  150. static struct task_struct *serio_task;
  151. static int serio_queue_event(void *object, struct module *owner,
  152. enum serio_event_type event_type)
  153. {
  154. unsigned long flags;
  155. struct serio_event *event;
  156. int retval = 0;
  157. spin_lock_irqsave(&serio_event_lock, flags);
  158. /*
  159. * Scan event list for the other events for the same serio port,
  160. * starting with the most recent one. If event is the same we
  161. * do not need add new one. If event is of different type we
  162. * need to add this event and should not look further because
  163. * we need to preseve sequence of distinct events.
  164. */
  165. list_for_each_entry_reverse(event, &serio_event_list, node) {
  166. if (event->object == object) {
  167. if (event->type == event_type)
  168. goto out;
  169. break;
  170. }
  171. }
  172. event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
  173. if (!event) {
  174. printk(KERN_ERR
  175. "serio: Not enough memory to queue event %d\n",
  176. event_type);
  177. retval = -ENOMEM;
  178. goto out;
  179. }
  180. if (!try_module_get(owner)) {
  181. printk(KERN_WARNING
  182. "serio: Can't get module reference, dropping event %d\n",
  183. event_type);
  184. kfree(event);
  185. retval = -EINVAL;
  186. goto out;
  187. }
  188. event->type = event_type;
  189. event->object = object;
  190. event->owner = owner;
  191. list_add_tail(&event->node, &serio_event_list);
  192. wake_up(&serio_wait);
  193. out:
  194. spin_unlock_irqrestore(&serio_event_lock, flags);
  195. return retval;
  196. }
  197. static void serio_free_event(struct serio_event *event)
  198. {
  199. module_put(event->owner);
  200. kfree(event);
  201. }
  202. static void serio_remove_duplicate_events(struct serio_event *event)
  203. {
  204. struct list_head *node, *next;
  205. struct serio_event *e;
  206. unsigned long flags;
  207. spin_lock_irqsave(&serio_event_lock, flags);
  208. list_for_each_safe(node, next, &serio_event_list) {
  209. e = list_entry(node, struct serio_event, node);
  210. if (event->object == e->object) {
  211. /*
  212. * If this event is of different type we should not
  213. * look further - we only suppress duplicate events
  214. * that were sent back-to-back.
  215. */
  216. if (event->type != e->type)
  217. break;
  218. list_del_init(node);
  219. serio_free_event(e);
  220. }
  221. }
  222. spin_unlock_irqrestore(&serio_event_lock, flags);
  223. }
  224. static struct serio_event *serio_get_event(void)
  225. {
  226. struct serio_event *event;
  227. struct list_head *node;
  228. unsigned long flags;
  229. spin_lock_irqsave(&serio_event_lock, flags);
  230. if (list_empty(&serio_event_list)) {
  231. spin_unlock_irqrestore(&serio_event_lock, flags);
  232. return NULL;
  233. }
  234. node = serio_event_list.next;
  235. event = list_entry(node, struct serio_event, node);
  236. list_del_init(node);
  237. spin_unlock_irqrestore(&serio_event_lock, flags);
  238. return event;
  239. }
  240. static void serio_handle_event(void)
  241. {
  242. struct serio_event *event;
  243. mutex_lock(&serio_mutex);
  244. /*
  245. * Note that we handle only one event here to give swsusp
  246. * a chance to freeze kseriod thread. Serio events should
  247. * be pretty rare so we are not concerned about taking
  248. * performance hit.
  249. */
  250. if ((event = serio_get_event())) {
  251. switch (event->type) {
  252. case SERIO_REGISTER_PORT:
  253. serio_add_port(event->object);
  254. break;
  255. case SERIO_RECONNECT_PORT:
  256. serio_reconnect_port(event->object);
  257. break;
  258. case SERIO_RESCAN_PORT:
  259. serio_disconnect_port(event->object);
  260. serio_find_driver(event->object);
  261. break;
  262. case SERIO_ATTACH_DRIVER:
  263. serio_attach_driver(event->object);
  264. break;
  265. default:
  266. break;
  267. }
  268. serio_remove_duplicate_events(event);
  269. serio_free_event(event);
  270. }
  271. mutex_unlock(&serio_mutex);
  272. }
  273. /*
  274. * Remove all events that have been submitted for a given serio port.
  275. */
  276. static void serio_remove_pending_events(struct serio *serio)
  277. {
  278. struct list_head *node, *next;
  279. struct serio_event *event;
  280. unsigned long flags;
  281. spin_lock_irqsave(&serio_event_lock, flags);
  282. list_for_each_safe(node, next, &serio_event_list) {
  283. event = list_entry(node, struct serio_event, node);
  284. if (event->object == serio) {
  285. list_del_init(node);
  286. serio_free_event(event);
  287. }
  288. }
  289. spin_unlock_irqrestore(&serio_event_lock, flags);
  290. }
  291. /*
  292. * Destroy child serio port (if any) that has not been fully registered yet.
  293. *
  294. * Note that we rely on the fact that port can have only one child and therefore
  295. * only one child registration request can be pending. Additionally, children
  296. * are registered by driver's connect() handler so there can't be a grandchild
  297. * pending registration together with a child.
  298. */
  299. static struct serio *serio_get_pending_child(struct serio *parent)
  300. {
  301. struct serio_event *event;
  302. struct serio *serio, *child = NULL;
  303. unsigned long flags;
  304. spin_lock_irqsave(&serio_event_lock, flags);
  305. list_for_each_entry(event, &serio_event_list, node) {
  306. if (event->type == SERIO_REGISTER_PORT) {
  307. serio = event->object;
  308. if (serio->parent == parent) {
  309. child = serio;
  310. break;
  311. }
  312. }
  313. }
  314. spin_unlock_irqrestore(&serio_event_lock, flags);
  315. return child;
  316. }
  317. static int serio_thread(void *nothing)
  318. {
  319. set_freezable();
  320. do {
  321. serio_handle_event();
  322. wait_event_interruptible(serio_wait,
  323. kthread_should_stop() || !list_empty(&serio_event_list));
  324. try_to_freeze();
  325. } while (!kthread_should_stop());
  326. printk(KERN_DEBUG "serio: kseriod exiting\n");
  327. return 0;
  328. }
  329. /*
  330. * Serio port operations
  331. */
  332. static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
  333. {
  334. struct serio *serio = to_serio_port(dev);
  335. return sprintf(buf, "%s\n", serio->name);
  336. }
  337. static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  338. {
  339. struct serio *serio = to_serio_port(dev);
  340. return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
  341. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  342. }
  343. static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
  344. {
  345. struct serio *serio = to_serio_port(dev);
  346. return sprintf(buf, "%02x\n", serio->id.type);
  347. }
  348. static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
  349. {
  350. struct serio *serio = to_serio_port(dev);
  351. return sprintf(buf, "%02x\n", serio->id.proto);
  352. }
  353. static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
  354. {
  355. struct serio *serio = to_serio_port(dev);
  356. return sprintf(buf, "%02x\n", serio->id.id);
  357. }
  358. static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
  359. {
  360. struct serio *serio = to_serio_port(dev);
  361. return sprintf(buf, "%02x\n", serio->id.extra);
  362. }
  363. static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
  364. static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
  365. static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
  366. static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
  367. static struct attribute *serio_device_id_attrs[] = {
  368. &dev_attr_type.attr,
  369. &dev_attr_proto.attr,
  370. &dev_attr_id.attr,
  371. &dev_attr_extra.attr,
  372. NULL
  373. };
  374. static struct attribute_group serio_id_attr_group = {
  375. .name = "id",
  376. .attrs = serio_device_id_attrs,
  377. };
  378. static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  379. {
  380. struct serio *serio = to_serio_port(dev);
  381. struct device_driver *drv;
  382. int error;
  383. error = mutex_lock_interruptible(&serio_mutex);
  384. if (error)
  385. return error;
  386. if (!strncmp(buf, "none", count)) {
  387. serio_disconnect_port(serio);
  388. } else if (!strncmp(buf, "reconnect", count)) {
  389. serio_reconnect_port(serio);
  390. } else if (!strncmp(buf, "rescan", count)) {
  391. serio_disconnect_port(serio);
  392. serio_find_driver(serio);
  393. } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
  394. serio_disconnect_port(serio);
  395. error = serio_bind_driver(serio, to_serio_driver(drv));
  396. put_driver(drv);
  397. } else {
  398. error = -EINVAL;
  399. }
  400. mutex_unlock(&serio_mutex);
  401. return error ? error : count;
  402. }
  403. static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
  404. {
  405. struct serio *serio = to_serio_port(dev);
  406. return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
  407. }
  408. static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  409. {
  410. struct serio *serio = to_serio_port(dev);
  411. int retval;
  412. retval = count;
  413. if (!strncmp(buf, "manual", count)) {
  414. serio->manual_bind = 1;
  415. } else if (!strncmp(buf, "auto", count)) {
  416. serio->manual_bind = 0;
  417. } else {
  418. retval = -EINVAL;
  419. }
  420. return retval;
  421. }
  422. static struct device_attribute serio_device_attrs[] = {
  423. __ATTR(description, S_IRUGO, serio_show_description, NULL),
  424. __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
  425. __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
  426. __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
  427. __ATTR_NULL
  428. };
  429. static void serio_release_port(struct device *dev)
  430. {
  431. struct serio *serio = to_serio_port(dev);
  432. kfree(serio);
  433. module_put(THIS_MODULE);
  434. }
  435. /*
  436. * Prepare serio port for registration.
  437. */
  438. static void serio_init_port(struct serio *serio)
  439. {
  440. static atomic_t serio_no = ATOMIC_INIT(0);
  441. __module_get(THIS_MODULE);
  442. INIT_LIST_HEAD(&serio->node);
  443. spin_lock_init(&serio->lock);
  444. mutex_init(&serio->drv_mutex);
  445. device_initialize(&serio->dev);
  446. snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
  447. "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
  448. serio->dev.bus = &serio_bus;
  449. serio->dev.release = serio_release_port;
  450. if (serio->parent) {
  451. serio->dev.parent = &serio->parent->dev;
  452. serio->depth = serio->parent->depth + 1;
  453. } else
  454. serio->depth = 0;
  455. lockdep_set_subclass(&serio->lock, serio->depth);
  456. }
  457. /*
  458. * Complete serio port registration.
  459. * Driver core will attempt to find appropriate driver for the port.
  460. */
  461. static void serio_add_port(struct serio *serio)
  462. {
  463. int error;
  464. if (serio->parent) {
  465. serio_pause_rx(serio->parent);
  466. serio->parent->child = serio;
  467. serio_continue_rx(serio->parent);
  468. }
  469. list_add_tail(&serio->node, &serio_list);
  470. if (serio->start)
  471. serio->start(serio);
  472. error = device_add(&serio->dev);
  473. if (error)
  474. printk(KERN_ERR
  475. "serio: device_add() failed for %s (%s), error: %d\n",
  476. serio->phys, serio->name, error);
  477. else {
  478. serio->registered = 1;
  479. error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
  480. if (error)
  481. printk(KERN_ERR
  482. "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
  483. serio->phys, serio->name, error);
  484. }
  485. }
  486. /*
  487. * serio_destroy_port() completes deregistration process and removes
  488. * port from the system
  489. */
  490. static void serio_destroy_port(struct serio *serio)
  491. {
  492. struct serio *child;
  493. child = serio_get_pending_child(serio);
  494. if (child) {
  495. serio_remove_pending_events(child);
  496. put_device(&child->dev);
  497. }
  498. if (serio->stop)
  499. serio->stop(serio);
  500. if (serio->parent) {
  501. serio_pause_rx(serio->parent);
  502. serio->parent->child = NULL;
  503. serio_continue_rx(serio->parent);
  504. serio->parent = NULL;
  505. }
  506. if (serio->registered) {
  507. sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
  508. device_del(&serio->dev);
  509. serio->registered = 0;
  510. }
  511. list_del_init(&serio->node);
  512. serio_remove_pending_events(serio);
  513. put_device(&serio->dev);
  514. }
  515. /*
  516. * Reconnect serio port and all its children (re-initialize attached devices)
  517. */
  518. static void serio_reconnect_port(struct serio *serio)
  519. {
  520. do {
  521. if (serio_reconnect_driver(serio)) {
  522. serio_disconnect_port(serio);
  523. serio_find_driver(serio);
  524. /* Ok, old children are now gone, we are done */
  525. break;
  526. }
  527. serio = serio->child;
  528. } while (serio);
  529. }
  530. /*
  531. * serio_disconnect_port() unbinds a port from its driver. As a side effect
  532. * all child ports are unbound and destroyed.
  533. */
  534. static void serio_disconnect_port(struct serio *serio)
  535. {
  536. struct serio *s, *parent;
  537. if (serio->child) {
  538. /*
  539. * Children ports should be disconnected and destroyed
  540. * first, staring with the leaf one, since we don't want
  541. * to do recursion
  542. */
  543. for (s = serio; s->child; s = s->child)
  544. /* empty */;
  545. do {
  546. parent = s->parent;
  547. device_release_driver(&s->dev);
  548. serio_destroy_port(s);
  549. } while ((s = parent) != serio);
  550. }
  551. /*
  552. * Ok, no children left, now disconnect this port
  553. */
  554. device_release_driver(&serio->dev);
  555. }
  556. void serio_rescan(struct serio *serio)
  557. {
  558. serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
  559. }
  560. void serio_reconnect(struct serio *serio)
  561. {
  562. serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
  563. }
  564. /*
  565. * Submits register request to kseriod for subsequent execution.
  566. * Note that port registration is always asynchronous.
  567. */
  568. void __serio_register_port(struct serio *serio, struct module *owner)
  569. {
  570. serio_init_port(serio);
  571. serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
  572. }
  573. /*
  574. * Synchronously unregisters serio port.
  575. */
  576. void serio_unregister_port(struct serio *serio)
  577. {
  578. mutex_lock(&serio_mutex);
  579. serio_disconnect_port(serio);
  580. serio_destroy_port(serio);
  581. mutex_unlock(&serio_mutex);
  582. }
  583. /*
  584. * Safely unregisters child port if one is present.
  585. */
  586. void serio_unregister_child_port(struct serio *serio)
  587. {
  588. mutex_lock(&serio_mutex);
  589. if (serio->child) {
  590. serio_disconnect_port(serio->child);
  591. serio_destroy_port(serio->child);
  592. }
  593. mutex_unlock(&serio_mutex);
  594. }
  595. /*
  596. * Serio driver operations
  597. */
  598. static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
  599. {
  600. struct serio_driver *driver = to_serio_driver(drv);
  601. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  602. }
  603. static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
  604. {
  605. struct serio_driver *serio_drv = to_serio_driver(drv);
  606. return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
  607. }
  608. static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
  609. {
  610. struct serio_driver *serio_drv = to_serio_driver(drv);
  611. int retval;
  612. retval = count;
  613. if (!strncmp(buf, "manual", count)) {
  614. serio_drv->manual_bind = 1;
  615. } else if (!strncmp(buf, "auto", count)) {
  616. serio_drv->manual_bind = 0;
  617. } else {
  618. retval = -EINVAL;
  619. }
  620. return retval;
  621. }
  622. static struct driver_attribute serio_driver_attrs[] = {
  623. __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
  624. __ATTR(bind_mode, S_IWUSR | S_IRUGO,
  625. serio_driver_show_bind_mode, serio_driver_set_bind_mode),
  626. __ATTR_NULL
  627. };
  628. static int serio_driver_probe(struct device *dev)
  629. {
  630. struct serio *serio = to_serio_port(dev);
  631. struct serio_driver *drv = to_serio_driver(dev->driver);
  632. return serio_connect_driver(serio, drv);
  633. }
  634. static int serio_driver_remove(struct device *dev)
  635. {
  636. struct serio *serio = to_serio_port(dev);
  637. serio_disconnect_driver(serio);
  638. return 0;
  639. }
  640. static void serio_cleanup(struct serio *serio)
  641. {
  642. mutex_lock(&serio->drv_mutex);
  643. if (serio->drv && serio->drv->cleanup)
  644. serio->drv->cleanup(serio);
  645. mutex_unlock(&serio->drv_mutex);
  646. }
  647. static void serio_shutdown(struct device *dev)
  648. {
  649. struct serio *serio = to_serio_port(dev);
  650. serio_cleanup(serio);
  651. }
  652. static void serio_attach_driver(struct serio_driver *drv)
  653. {
  654. int error;
  655. error = driver_attach(&drv->driver);
  656. if (error)
  657. printk(KERN_WARNING
  658. "serio: driver_attach() failed for %s with error %d\n",
  659. drv->driver.name, error);
  660. }
  661. int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
  662. {
  663. int manual_bind = drv->manual_bind;
  664. int error;
  665. drv->driver.bus = &serio_bus;
  666. drv->driver.owner = owner;
  667. drv->driver.mod_name = mod_name;
  668. /*
  669. * Temporarily disable automatic binding because probing
  670. * takes long time and we are better off doing it in kseriod
  671. */
  672. drv->manual_bind = 1;
  673. error = driver_register(&drv->driver);
  674. if (error) {
  675. printk(KERN_ERR
  676. "serio: driver_register() failed for %s, error: %d\n",
  677. drv->driver.name, error);
  678. return error;
  679. }
  680. /*
  681. * Restore original bind mode and let kseriod bind the
  682. * driver to free ports
  683. */
  684. if (!manual_bind) {
  685. drv->manual_bind = 0;
  686. error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
  687. if (error) {
  688. driver_unregister(&drv->driver);
  689. return error;
  690. }
  691. }
  692. return 0;
  693. }
  694. void serio_unregister_driver(struct serio_driver *drv)
  695. {
  696. struct serio *serio;
  697. mutex_lock(&serio_mutex);
  698. drv->manual_bind = 1; /* so serio_find_driver ignores it */
  699. start_over:
  700. list_for_each_entry(serio, &serio_list, node) {
  701. if (serio->drv == drv) {
  702. serio_disconnect_port(serio);
  703. serio_find_driver(serio);
  704. /* we could've deleted some ports, restart */
  705. goto start_over;
  706. }
  707. }
  708. driver_unregister(&drv->driver);
  709. mutex_unlock(&serio_mutex);
  710. }
  711. static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
  712. {
  713. serio_pause_rx(serio);
  714. serio->drv = drv;
  715. serio_continue_rx(serio);
  716. }
  717. static int serio_bus_match(struct device *dev, struct device_driver *drv)
  718. {
  719. struct serio *serio = to_serio_port(dev);
  720. struct serio_driver *serio_drv = to_serio_driver(drv);
  721. if (serio->manual_bind || serio_drv->manual_bind)
  722. return 0;
  723. return serio_match_port(serio_drv->id_table, serio);
  724. }
  725. #ifdef CONFIG_HOTPLUG
  726. #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
  727. do { \
  728. int err = add_uevent_var(envp, num_envp, &i, \
  729. buffer, buffer_size, &len, \
  730. fmt, val); \
  731. if (err) \
  732. return err; \
  733. } while (0)
  734. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  735. {
  736. struct serio *serio;
  737. int i = 0;
  738. int len = 0;
  739. if (!dev)
  740. return -ENODEV;
  741. serio = to_serio_port(dev);
  742. SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
  743. SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
  744. SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
  745. SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
  746. SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
  747. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  748. envp[i] = NULL;
  749. return 0;
  750. }
  751. #undef SERIO_ADD_UEVENT_VAR
  752. #else
  753. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  754. {
  755. return -ENODEV;
  756. }
  757. #endif /* CONFIG_HOTPLUG */
  758. #ifdef CONFIG_PM
  759. static int serio_suspend(struct device *dev, pm_message_t state)
  760. {
  761. if (dev->power.power_state.event != state.event) {
  762. if (state.event == PM_EVENT_SUSPEND)
  763. serio_cleanup(to_serio_port(dev));
  764. dev->power.power_state = state;
  765. }
  766. return 0;
  767. }
  768. static int serio_resume(struct device *dev)
  769. {
  770. struct serio *serio = to_serio_port(dev);
  771. if (dev->power.power_state.event != PM_EVENT_ON &&
  772. serio_reconnect_driver(serio)) {
  773. /*
  774. * Driver re-probing can take a while, so better let kseriod
  775. * deal with it.
  776. */
  777. serio_rescan(serio);
  778. }
  779. dev->power.power_state = PMSG_ON;
  780. return 0;
  781. }
  782. #endif /* CONFIG_PM */
  783. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  784. int serio_open(struct serio *serio, struct serio_driver *drv)
  785. {
  786. serio_set_drv(serio, drv);
  787. if (serio->open && serio->open(serio)) {
  788. serio_set_drv(serio, NULL);
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  794. void serio_close(struct serio *serio)
  795. {
  796. if (serio->close)
  797. serio->close(serio);
  798. serio_set_drv(serio, NULL);
  799. }
  800. irqreturn_t serio_interrupt(struct serio *serio,
  801. unsigned char data, unsigned int dfl)
  802. {
  803. unsigned long flags;
  804. irqreturn_t ret = IRQ_NONE;
  805. spin_lock_irqsave(&serio->lock, flags);
  806. if (likely(serio->drv)) {
  807. ret = serio->drv->interrupt(serio, data, dfl);
  808. } else if (!dfl && serio->registered) {
  809. serio_rescan(serio);
  810. ret = IRQ_HANDLED;
  811. }
  812. spin_unlock_irqrestore(&serio->lock, flags);
  813. return ret;
  814. }
  815. static struct bus_type serio_bus = {
  816. .name = "serio",
  817. .dev_attrs = serio_device_attrs,
  818. .drv_attrs = serio_driver_attrs,
  819. .match = serio_bus_match,
  820. .uevent = serio_uevent,
  821. .probe = serio_driver_probe,
  822. .remove = serio_driver_remove,
  823. .shutdown = serio_shutdown,
  824. #ifdef CONFIG_PM
  825. .suspend = serio_suspend,
  826. .resume = serio_resume,
  827. #endif
  828. };
  829. static int __init serio_init(void)
  830. {
  831. int error;
  832. error = bus_register(&serio_bus);
  833. if (error) {
  834. printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
  835. return error;
  836. }
  837. serio_task = kthread_run(serio_thread, NULL, "kseriod");
  838. if (IS_ERR(serio_task)) {
  839. bus_unregister(&serio_bus);
  840. error = PTR_ERR(serio_task);
  841. printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
  842. return error;
  843. }
  844. return 0;
  845. }
  846. static void __exit serio_exit(void)
  847. {
  848. bus_unregister(&serio_bus);
  849. kthread_stop(serio_task);
  850. }
  851. subsys_initcall(serio_init);
  852. module_exit(serio_exit);