serio.c 25 KB

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