serio.c 25 KB

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