serio.c 25 KB

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