serio.c 24 KB

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