serio.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. do {
  320. serio_handle_event();
  321. wait_event_interruptible(serio_wait,
  322. kthread_should_stop() || !list_empty(&serio_event_list));
  323. try_to_freeze();
  324. } while (!kthread_should_stop());
  325. printk(KERN_DEBUG "serio: kseriod exiting\n");
  326. return 0;
  327. }
  328. /*
  329. * Serio port operations
  330. */
  331. static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
  332. {
  333. struct serio *serio = to_serio_port(dev);
  334. return sprintf(buf, "%s\n", serio->name);
  335. }
  336. static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  337. {
  338. struct serio *serio = to_serio_port(dev);
  339. return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
  340. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  341. }
  342. static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
  343. {
  344. struct serio *serio = to_serio_port(dev);
  345. return sprintf(buf, "%02x\n", serio->id.type);
  346. }
  347. static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
  348. {
  349. struct serio *serio = to_serio_port(dev);
  350. return sprintf(buf, "%02x\n", serio->id.proto);
  351. }
  352. static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
  353. {
  354. struct serio *serio = to_serio_port(dev);
  355. return sprintf(buf, "%02x\n", serio->id.id);
  356. }
  357. static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
  358. {
  359. struct serio *serio = to_serio_port(dev);
  360. return sprintf(buf, "%02x\n", serio->id.extra);
  361. }
  362. static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
  363. static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
  364. static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
  365. static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
  366. static struct attribute *serio_device_id_attrs[] = {
  367. &dev_attr_type.attr,
  368. &dev_attr_proto.attr,
  369. &dev_attr_id.attr,
  370. &dev_attr_extra.attr,
  371. NULL
  372. };
  373. static struct attribute_group serio_id_attr_group = {
  374. .name = "id",
  375. .attrs = serio_device_id_attrs,
  376. };
  377. static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  378. {
  379. struct serio *serio = to_serio_port(dev);
  380. struct device_driver *drv;
  381. int error;
  382. error = mutex_lock_interruptible(&serio_mutex);
  383. if (error)
  384. return error;
  385. if (!strncmp(buf, "none", count)) {
  386. serio_disconnect_port(serio);
  387. } else if (!strncmp(buf, "reconnect", count)) {
  388. serio_reconnect_port(serio);
  389. } else if (!strncmp(buf, "rescan", count)) {
  390. serio_disconnect_port(serio);
  391. serio_find_driver(serio);
  392. } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
  393. serio_disconnect_port(serio);
  394. error = serio_bind_driver(serio, to_serio_driver(drv));
  395. put_driver(drv);
  396. } else {
  397. error = -EINVAL;
  398. }
  399. mutex_unlock(&serio_mutex);
  400. return error ? error : count;
  401. }
  402. static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
  403. {
  404. struct serio *serio = to_serio_port(dev);
  405. return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
  406. }
  407. static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  408. {
  409. struct serio *serio = to_serio_port(dev);
  410. int retval;
  411. retval = count;
  412. if (!strncmp(buf, "manual", count)) {
  413. serio->manual_bind = 1;
  414. } else if (!strncmp(buf, "auto", count)) {
  415. serio->manual_bind = 0;
  416. } else {
  417. retval = -EINVAL;
  418. }
  419. return retval;
  420. }
  421. static struct device_attribute serio_device_attrs[] = {
  422. __ATTR(description, S_IRUGO, serio_show_description, NULL),
  423. __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
  424. __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
  425. __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
  426. __ATTR_NULL
  427. };
  428. static void serio_release_port(struct device *dev)
  429. {
  430. struct serio *serio = to_serio_port(dev);
  431. kfree(serio);
  432. module_put(THIS_MODULE);
  433. }
  434. /*
  435. * Prepare serio port for registration.
  436. */
  437. static void serio_init_port(struct serio *serio)
  438. {
  439. static atomic_t serio_no = ATOMIC_INIT(0);
  440. __module_get(THIS_MODULE);
  441. INIT_LIST_HEAD(&serio->node);
  442. spin_lock_init(&serio->lock);
  443. mutex_init(&serio->drv_mutex);
  444. device_initialize(&serio->dev);
  445. snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
  446. "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
  447. serio->dev.bus = &serio_bus;
  448. serio->dev.release = serio_release_port;
  449. if (serio->parent) {
  450. serio->dev.parent = &serio->parent->dev;
  451. serio->depth = serio->parent->depth + 1;
  452. } else
  453. serio->depth = 0;
  454. lockdep_set_subclass(&serio->lock, serio->depth);
  455. }
  456. /*
  457. * Complete serio port registration.
  458. * Driver core will attempt to find appropriate driver for the port.
  459. */
  460. static void serio_add_port(struct serio *serio)
  461. {
  462. int error;
  463. if (serio->parent) {
  464. serio_pause_rx(serio->parent);
  465. serio->parent->child = serio;
  466. serio_continue_rx(serio->parent);
  467. }
  468. list_add_tail(&serio->node, &serio_list);
  469. if (serio->start)
  470. serio->start(serio);
  471. error = device_add(&serio->dev);
  472. if (error)
  473. printk(KERN_ERR
  474. "serio: device_add() failed for %s (%s), error: %d\n",
  475. serio->phys, serio->name, error);
  476. else {
  477. serio->registered = 1;
  478. error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
  479. if (error)
  480. printk(KERN_ERR
  481. "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
  482. serio->phys, serio->name, error);
  483. }
  484. }
  485. /*
  486. * serio_destroy_port() completes deregistration process and removes
  487. * port from the system
  488. */
  489. static void serio_destroy_port(struct serio *serio)
  490. {
  491. struct serio *child;
  492. child = serio_get_pending_child(serio);
  493. if (child) {
  494. serio_remove_pending_events(child);
  495. put_device(&child->dev);
  496. }
  497. if (serio->stop)
  498. serio->stop(serio);
  499. if (serio->parent) {
  500. serio_pause_rx(serio->parent);
  501. serio->parent->child = NULL;
  502. serio_continue_rx(serio->parent);
  503. serio->parent = NULL;
  504. }
  505. if (serio->registered) {
  506. sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
  507. device_del(&serio->dev);
  508. serio->registered = 0;
  509. }
  510. list_del_init(&serio->node);
  511. serio_remove_pending_events(serio);
  512. put_device(&serio->dev);
  513. }
  514. /*
  515. * Reconnect serio port and all its children (re-initialize attached devices)
  516. */
  517. static void serio_reconnect_port(struct serio *serio)
  518. {
  519. do {
  520. if (serio_reconnect_driver(serio)) {
  521. serio_disconnect_port(serio);
  522. serio_find_driver(serio);
  523. /* Ok, old children are now gone, we are done */
  524. break;
  525. }
  526. serio = serio->child;
  527. } while (serio);
  528. }
  529. /*
  530. * serio_disconnect_port() unbinds a port from its driver. As a side effect
  531. * all child ports are unbound and destroyed.
  532. */
  533. static void serio_disconnect_port(struct serio *serio)
  534. {
  535. struct serio *s, *parent;
  536. if (serio->child) {
  537. /*
  538. * Children ports should be disconnected and destroyed
  539. * first, staring with the leaf one, since we don't want
  540. * to do recursion
  541. */
  542. for (s = serio; s->child; s = s->child)
  543. /* empty */;
  544. do {
  545. parent = s->parent;
  546. device_release_driver(&s->dev);
  547. serio_destroy_port(s);
  548. } while ((s = parent) != serio);
  549. }
  550. /*
  551. * Ok, no children left, now disconnect this port
  552. */
  553. device_release_driver(&serio->dev);
  554. }
  555. void serio_rescan(struct serio *serio)
  556. {
  557. serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
  558. }
  559. void serio_reconnect(struct serio *serio)
  560. {
  561. serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
  562. }
  563. /*
  564. * Submits register request to kseriod for subsequent execution.
  565. * Note that port registration is always asynchronous.
  566. */
  567. void __serio_register_port(struct serio *serio, struct module *owner)
  568. {
  569. serio_init_port(serio);
  570. serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
  571. }
  572. /*
  573. * Synchronously unregisters serio port.
  574. */
  575. void serio_unregister_port(struct serio *serio)
  576. {
  577. mutex_lock(&serio_mutex);
  578. serio_disconnect_port(serio);
  579. serio_destroy_port(serio);
  580. mutex_unlock(&serio_mutex);
  581. }
  582. /*
  583. * Safely unregisters child port if one is present.
  584. */
  585. void serio_unregister_child_port(struct serio *serio)
  586. {
  587. mutex_lock(&serio_mutex);
  588. if (serio->child) {
  589. serio_disconnect_port(serio->child);
  590. serio_destroy_port(serio->child);
  591. }
  592. mutex_unlock(&serio_mutex);
  593. }
  594. /*
  595. * Serio driver operations
  596. */
  597. static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
  598. {
  599. struct serio_driver *driver = to_serio_driver(drv);
  600. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  601. }
  602. static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
  603. {
  604. struct serio_driver *serio_drv = to_serio_driver(drv);
  605. return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
  606. }
  607. static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
  608. {
  609. struct serio_driver *serio_drv = to_serio_driver(drv);
  610. int retval;
  611. retval = count;
  612. if (!strncmp(buf, "manual", count)) {
  613. serio_drv->manual_bind = 1;
  614. } else if (!strncmp(buf, "auto", count)) {
  615. serio_drv->manual_bind = 0;
  616. } else {
  617. retval = -EINVAL;
  618. }
  619. return retval;
  620. }
  621. static struct driver_attribute serio_driver_attrs[] = {
  622. __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
  623. __ATTR(bind_mode, S_IWUSR | S_IRUGO,
  624. serio_driver_show_bind_mode, serio_driver_set_bind_mode),
  625. __ATTR_NULL
  626. };
  627. static int serio_driver_probe(struct device *dev)
  628. {
  629. struct serio *serio = to_serio_port(dev);
  630. struct serio_driver *drv = to_serio_driver(dev->driver);
  631. return serio_connect_driver(serio, drv);
  632. }
  633. static int serio_driver_remove(struct device *dev)
  634. {
  635. struct serio *serio = to_serio_port(dev);
  636. serio_disconnect_driver(serio);
  637. return 0;
  638. }
  639. static void serio_cleanup(struct serio *serio)
  640. {
  641. if (serio->drv && serio->drv->cleanup)
  642. serio->drv->cleanup(serio);
  643. }
  644. static void serio_shutdown(struct device *dev)
  645. {
  646. struct serio *serio = to_serio_port(dev);
  647. serio_cleanup(serio);
  648. }
  649. static void serio_attach_driver(struct serio_driver *drv)
  650. {
  651. int error;
  652. error = driver_attach(&drv->driver);
  653. if (error)
  654. printk(KERN_WARNING
  655. "serio: driver_attach() failed for %s with error %d\n",
  656. drv->driver.name, error);
  657. }
  658. int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
  659. {
  660. int manual_bind = drv->manual_bind;
  661. int error;
  662. drv->driver.bus = &serio_bus;
  663. drv->driver.owner = owner;
  664. drv->driver.mod_name = mod_name;
  665. /*
  666. * Temporarily disable automatic binding because probing
  667. * takes long time and we are better off doing it in kseriod
  668. */
  669. drv->manual_bind = 1;
  670. error = driver_register(&drv->driver);
  671. if (error) {
  672. printk(KERN_ERR
  673. "serio: driver_register() failed for %s, error: %d\n",
  674. drv->driver.name, error);
  675. return error;
  676. }
  677. /*
  678. * Restore original bind mode and let kseriod bind the
  679. * driver to free ports
  680. */
  681. if (!manual_bind) {
  682. drv->manual_bind = 0;
  683. error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
  684. if (error) {
  685. driver_unregister(&drv->driver);
  686. return error;
  687. }
  688. }
  689. return 0;
  690. }
  691. void serio_unregister_driver(struct serio_driver *drv)
  692. {
  693. struct serio *serio;
  694. mutex_lock(&serio_mutex);
  695. drv->manual_bind = 1; /* so serio_find_driver ignores it */
  696. start_over:
  697. list_for_each_entry(serio, &serio_list, node) {
  698. if (serio->drv == drv) {
  699. serio_disconnect_port(serio);
  700. serio_find_driver(serio);
  701. /* we could've deleted some ports, restart */
  702. goto start_over;
  703. }
  704. }
  705. driver_unregister(&drv->driver);
  706. mutex_unlock(&serio_mutex);
  707. }
  708. static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
  709. {
  710. serio_pause_rx(serio);
  711. serio->drv = drv;
  712. serio_continue_rx(serio);
  713. }
  714. static int serio_bus_match(struct device *dev, struct device_driver *drv)
  715. {
  716. struct serio *serio = to_serio_port(dev);
  717. struct serio_driver *serio_drv = to_serio_driver(drv);
  718. if (serio->manual_bind || serio_drv->manual_bind)
  719. return 0;
  720. return serio_match_port(serio_drv->id_table, serio);
  721. }
  722. #ifdef CONFIG_HOTPLUG
  723. #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
  724. do { \
  725. int err = add_uevent_var(envp, num_envp, &i, \
  726. buffer, buffer_size, &len, \
  727. fmt, val); \
  728. if (err) \
  729. return err; \
  730. } while (0)
  731. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  732. {
  733. struct serio *serio;
  734. int i = 0;
  735. int len = 0;
  736. if (!dev)
  737. return -ENODEV;
  738. serio = to_serio_port(dev);
  739. SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
  740. SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
  741. SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
  742. SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
  743. SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
  744. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  745. envp[i] = NULL;
  746. return 0;
  747. }
  748. #undef SERIO_ADD_UEVENT_VAR
  749. #else
  750. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  751. {
  752. return -ENODEV;
  753. }
  754. #endif /* CONFIG_HOTPLUG */
  755. #ifdef CONFIG_PM
  756. static int serio_suspend(struct device *dev, pm_message_t state)
  757. {
  758. if (dev->power.power_state.event != state.event) {
  759. if (state.event == PM_EVENT_SUSPEND)
  760. serio_cleanup(to_serio_port(dev));
  761. dev->power.power_state = state;
  762. }
  763. return 0;
  764. }
  765. static int serio_resume(struct device *dev)
  766. {
  767. struct serio *serio = to_serio_port(dev);
  768. if (dev->power.power_state.event != PM_EVENT_ON &&
  769. serio_reconnect_driver(serio)) {
  770. /*
  771. * Driver re-probing can take a while, so better let kseriod
  772. * deal with it.
  773. */
  774. serio_rescan(serio);
  775. }
  776. dev->power.power_state = PMSG_ON;
  777. return 0;
  778. }
  779. #endif /* CONFIG_PM */
  780. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  781. int serio_open(struct serio *serio, struct serio_driver *drv)
  782. {
  783. serio_set_drv(serio, drv);
  784. if (serio->open && serio->open(serio)) {
  785. serio_set_drv(serio, NULL);
  786. return -1;
  787. }
  788. return 0;
  789. }
  790. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  791. void serio_close(struct serio *serio)
  792. {
  793. if (serio->close)
  794. serio->close(serio);
  795. serio_set_drv(serio, NULL);
  796. }
  797. irqreturn_t serio_interrupt(struct serio *serio,
  798. unsigned char data, unsigned int dfl)
  799. {
  800. unsigned long flags;
  801. irqreturn_t ret = IRQ_NONE;
  802. spin_lock_irqsave(&serio->lock, flags);
  803. if (likely(serio->drv)) {
  804. ret = serio->drv->interrupt(serio, data, dfl);
  805. } else if (!dfl && serio->registered) {
  806. serio_rescan(serio);
  807. ret = IRQ_HANDLED;
  808. }
  809. spin_unlock_irqrestore(&serio->lock, flags);
  810. return ret;
  811. }
  812. static struct bus_type serio_bus = {
  813. .name = "serio",
  814. .dev_attrs = serio_device_attrs,
  815. .drv_attrs = serio_driver_attrs,
  816. .match = serio_bus_match,
  817. .uevent = serio_uevent,
  818. .probe = serio_driver_probe,
  819. .remove = serio_driver_remove,
  820. .shutdown = serio_shutdown,
  821. #ifdef CONFIG_PM
  822. .suspend = serio_suspend,
  823. .resume = serio_resume,
  824. #endif
  825. };
  826. static int __init serio_init(void)
  827. {
  828. int error;
  829. error = bus_register(&serio_bus);
  830. if (error) {
  831. printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
  832. return error;
  833. }
  834. serio_task = kthread_run(serio_thread, NULL, "kseriod");
  835. if (IS_ERR(serio_task)) {
  836. bus_unregister(&serio_bus);
  837. error = PTR_ERR(serio_task);
  838. printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
  839. return error;
  840. }
  841. return 0;
  842. }
  843. static void __exit serio_exit(void)
  844. {
  845. bus_unregister(&serio_bus);
  846. kthread_stop(serio_task);
  847. }
  848. subsys_initcall(serio_init);
  849. module_exit(serio_exit);