virtio_console.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  1. /*
  2. * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
  3. * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
  4. * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/cdev.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/completion.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/freezer.h>
  26. #include <linux/fs.h>
  27. #include <linux/splice.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/init.h>
  30. #include <linux/list.h>
  31. #include <linux/poll.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/virtio.h>
  36. #include <linux/virtio_console.h>
  37. #include <linux/wait.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/module.h>
  40. #include "../tty/hvc/hvc_console.h"
  41. /*
  42. * This is a global struct for storing common data for all the devices
  43. * this driver handles.
  44. *
  45. * Mainly, it has a linked list for all the consoles in one place so
  46. * that callbacks from hvc for get_chars(), put_chars() work properly
  47. * across multiple devices and multiple ports per device.
  48. */
  49. struct ports_driver_data {
  50. /* Used for registering chardevs */
  51. struct class *class;
  52. /* Used for exporting per-port information to debugfs */
  53. struct dentry *debugfs_dir;
  54. /* List of all the devices we're handling */
  55. struct list_head portdevs;
  56. /* Number of devices this driver is handling */
  57. unsigned int index;
  58. /*
  59. * This is used to keep track of the number of hvc consoles
  60. * spawned by this driver. This number is given as the first
  61. * argument to hvc_alloc(). To correctly map an initial
  62. * console spawned via hvc_instantiate to the console being
  63. * hooked up via hvc_alloc, we need to pass the same vtermno.
  64. *
  65. * We also just assume the first console being initialised was
  66. * the first one that got used as the initial console.
  67. */
  68. unsigned int next_vtermno;
  69. /* All the console devices handled by this driver */
  70. struct list_head consoles;
  71. };
  72. static struct ports_driver_data pdrvdata;
  73. DEFINE_SPINLOCK(pdrvdata_lock);
  74. DECLARE_COMPLETION(early_console_added);
  75. /* This struct holds information that's relevant only for console ports */
  76. struct console {
  77. /* We'll place all consoles in a list in the pdrvdata struct */
  78. struct list_head list;
  79. /* The hvc device associated with this console port */
  80. struct hvc_struct *hvc;
  81. /* The size of the console */
  82. struct winsize ws;
  83. /*
  84. * This number identifies the number that we used to register
  85. * with hvc in hvc_instantiate() and hvc_alloc(); this is the
  86. * number passed on by the hvc callbacks to us to
  87. * differentiate between the other console ports handled by
  88. * this driver
  89. */
  90. u32 vtermno;
  91. };
  92. struct port_buffer {
  93. char *buf;
  94. /* size of the buffer in *buf above */
  95. size_t size;
  96. /* used length of the buffer */
  97. size_t len;
  98. /* offset in the buf from which to consume data */
  99. size_t offset;
  100. };
  101. /*
  102. * This is a per-device struct that stores data common to all the
  103. * ports for that device (vdev->priv).
  104. */
  105. struct ports_device {
  106. /* Next portdev in the list, head is in the pdrvdata struct */
  107. struct list_head list;
  108. /*
  109. * Workqueue handlers where we process deferred work after
  110. * notification
  111. */
  112. struct work_struct control_work;
  113. struct list_head ports;
  114. /* To protect the list of ports */
  115. spinlock_t ports_lock;
  116. /* To protect the vq operations for the control channel */
  117. spinlock_t cvq_lock;
  118. /* The current config space is stored here */
  119. struct virtio_console_config config;
  120. /* The virtio device we're associated with */
  121. struct virtio_device *vdev;
  122. /*
  123. * A couple of virtqueues for the control channel: one for
  124. * guest->host transfers, one for host->guest transfers
  125. */
  126. struct virtqueue *c_ivq, *c_ovq;
  127. /* Array of per-port IO virtqueues */
  128. struct virtqueue **in_vqs, **out_vqs;
  129. /* Used for numbering devices for sysfs and debugfs */
  130. unsigned int drv_index;
  131. /* Major number for this device. Ports will be created as minors. */
  132. int chr_major;
  133. };
  134. struct port_stats {
  135. unsigned long bytes_sent, bytes_received, bytes_discarded;
  136. };
  137. /* This struct holds the per-port data */
  138. struct port {
  139. /* Next port in the list, head is in the ports_device */
  140. struct list_head list;
  141. /* Pointer to the parent virtio_console device */
  142. struct ports_device *portdev;
  143. /* The current buffer from which data has to be fed to readers */
  144. struct port_buffer *inbuf;
  145. /*
  146. * To protect the operations on the in_vq associated with this
  147. * port. Has to be a spinlock because it can be called from
  148. * interrupt context (get_char()).
  149. */
  150. spinlock_t inbuf_lock;
  151. /* Protect the operations on the out_vq. */
  152. spinlock_t outvq_lock;
  153. /* The IO vqs for this port */
  154. struct virtqueue *in_vq, *out_vq;
  155. /* File in the debugfs directory that exposes this port's information */
  156. struct dentry *debugfs_file;
  157. /*
  158. * Keep count of the bytes sent, received and discarded for
  159. * this port for accounting and debugging purposes. These
  160. * counts are not reset across port open / close events.
  161. */
  162. struct port_stats stats;
  163. /*
  164. * The entries in this struct will be valid if this port is
  165. * hooked up to an hvc console
  166. */
  167. struct console cons;
  168. /* Each port associates with a separate char device */
  169. struct cdev *cdev;
  170. struct device *dev;
  171. /* Reference-counting to handle port hot-unplugs and file operations */
  172. struct kref kref;
  173. /* A waitqueue for poll() or blocking read operations */
  174. wait_queue_head_t waitqueue;
  175. /* The 'name' of the port that we expose via sysfs properties */
  176. char *name;
  177. /* We can notify apps of host connect / disconnect events via SIGIO */
  178. struct fasync_struct *async_queue;
  179. /* The 'id' to identify the port with the Host */
  180. u32 id;
  181. bool outvq_full;
  182. /* Is the host device open */
  183. bool host_connected;
  184. /* We should allow only one process to open a port */
  185. bool guest_connected;
  186. };
  187. /* This is the very early arch-specified put chars function. */
  188. static int (*early_put_chars)(u32, const char *, int);
  189. static struct port *find_port_by_vtermno(u32 vtermno)
  190. {
  191. struct port *port;
  192. struct console *cons;
  193. unsigned long flags;
  194. spin_lock_irqsave(&pdrvdata_lock, flags);
  195. list_for_each_entry(cons, &pdrvdata.consoles, list) {
  196. if (cons->vtermno == vtermno) {
  197. port = container_of(cons, struct port, cons);
  198. goto out;
  199. }
  200. }
  201. port = NULL;
  202. out:
  203. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  204. return port;
  205. }
  206. static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
  207. dev_t dev)
  208. {
  209. struct port *port;
  210. unsigned long flags;
  211. spin_lock_irqsave(&portdev->ports_lock, flags);
  212. list_for_each_entry(port, &portdev->ports, list)
  213. if (port->cdev->dev == dev)
  214. goto out;
  215. port = NULL;
  216. out:
  217. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  218. return port;
  219. }
  220. static struct port *find_port_by_devt(dev_t dev)
  221. {
  222. struct ports_device *portdev;
  223. struct port *port;
  224. unsigned long flags;
  225. spin_lock_irqsave(&pdrvdata_lock, flags);
  226. list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
  227. port = find_port_by_devt_in_portdev(portdev, dev);
  228. if (port)
  229. goto out;
  230. }
  231. port = NULL;
  232. out:
  233. spin_unlock_irqrestore(&pdrvdata_lock, flags);
  234. return port;
  235. }
  236. static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
  237. {
  238. struct port *port;
  239. unsigned long flags;
  240. spin_lock_irqsave(&portdev->ports_lock, flags);
  241. list_for_each_entry(port, &portdev->ports, list)
  242. if (port->id == id)
  243. goto out;
  244. port = NULL;
  245. out:
  246. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  247. return port;
  248. }
  249. static struct port *find_port_by_vq(struct ports_device *portdev,
  250. struct virtqueue *vq)
  251. {
  252. struct port *port;
  253. unsigned long flags;
  254. spin_lock_irqsave(&portdev->ports_lock, flags);
  255. list_for_each_entry(port, &portdev->ports, list)
  256. if (port->in_vq == vq || port->out_vq == vq)
  257. goto out;
  258. port = NULL;
  259. out:
  260. spin_unlock_irqrestore(&portdev->ports_lock, flags);
  261. return port;
  262. }
  263. static bool is_console_port(struct port *port)
  264. {
  265. if (port->cons.hvc)
  266. return true;
  267. return false;
  268. }
  269. static inline bool use_multiport(struct ports_device *portdev)
  270. {
  271. /*
  272. * This condition can be true when put_chars is called from
  273. * early_init
  274. */
  275. if (!portdev->vdev)
  276. return 0;
  277. return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  278. }
  279. static void free_buf(struct port_buffer *buf)
  280. {
  281. kfree(buf->buf);
  282. kfree(buf);
  283. }
  284. static struct port_buffer *alloc_buf(size_t buf_size)
  285. {
  286. struct port_buffer *buf;
  287. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  288. if (!buf)
  289. goto fail;
  290. buf->buf = kzalloc(buf_size, GFP_KERNEL);
  291. if (!buf->buf)
  292. goto free_buf;
  293. buf->len = 0;
  294. buf->offset = 0;
  295. buf->size = buf_size;
  296. return buf;
  297. free_buf:
  298. kfree(buf);
  299. fail:
  300. return NULL;
  301. }
  302. /* Callers should take appropriate locks */
  303. static struct port_buffer *get_inbuf(struct port *port)
  304. {
  305. struct port_buffer *buf;
  306. unsigned int len;
  307. if (port->inbuf)
  308. return port->inbuf;
  309. buf = virtqueue_get_buf(port->in_vq, &len);
  310. if (buf) {
  311. buf->len = len;
  312. buf->offset = 0;
  313. port->stats.bytes_received += len;
  314. }
  315. return buf;
  316. }
  317. /*
  318. * Create a scatter-gather list representing our input buffer and put
  319. * it in the queue.
  320. *
  321. * Callers should take appropriate locks.
  322. */
  323. static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
  324. {
  325. struct scatterlist sg[1];
  326. int ret;
  327. sg_init_one(sg, buf->buf, buf->size);
  328. ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
  329. virtqueue_kick(vq);
  330. return ret;
  331. }
  332. /* Discard any unread data this port has. Callers lockers. */
  333. static void discard_port_data(struct port *port)
  334. {
  335. struct port_buffer *buf;
  336. unsigned int err;
  337. if (!port->portdev) {
  338. /* Device has been unplugged. vqs are already gone. */
  339. return;
  340. }
  341. buf = get_inbuf(port);
  342. err = 0;
  343. while (buf) {
  344. port->stats.bytes_discarded += buf->len - buf->offset;
  345. if (add_inbuf(port->in_vq, buf) < 0) {
  346. err++;
  347. free_buf(buf);
  348. }
  349. port->inbuf = NULL;
  350. buf = get_inbuf(port);
  351. }
  352. if (err)
  353. dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
  354. err);
  355. }
  356. static bool port_has_data(struct port *port)
  357. {
  358. unsigned long flags;
  359. bool ret;
  360. ret = false;
  361. spin_lock_irqsave(&port->inbuf_lock, flags);
  362. port->inbuf = get_inbuf(port);
  363. if (port->inbuf)
  364. ret = true;
  365. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  366. return ret;
  367. }
  368. static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
  369. unsigned int event, unsigned int value)
  370. {
  371. struct scatterlist sg[1];
  372. struct virtio_console_control cpkt;
  373. struct virtqueue *vq;
  374. unsigned int len;
  375. if (!use_multiport(portdev))
  376. return 0;
  377. cpkt.id = port_id;
  378. cpkt.event = event;
  379. cpkt.value = value;
  380. vq = portdev->c_ovq;
  381. sg_init_one(sg, &cpkt, sizeof(cpkt));
  382. if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) {
  383. virtqueue_kick(vq);
  384. while (!virtqueue_get_buf(vq, &len))
  385. cpu_relax();
  386. }
  387. return 0;
  388. }
  389. static ssize_t send_control_msg(struct port *port, unsigned int event,
  390. unsigned int value)
  391. {
  392. /* Did the port get unplugged before userspace closed it? */
  393. if (port->portdev)
  394. return __send_control_msg(port->portdev, port->id, event, value);
  395. return 0;
  396. }
  397. struct buffer_token {
  398. union {
  399. void *buf;
  400. struct scatterlist *sg;
  401. } u;
  402. /* If sgpages == 0 then buf is used, else sg is used */
  403. unsigned int sgpages;
  404. };
  405. static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages)
  406. {
  407. int i;
  408. struct page *page;
  409. for (i = 0; i < nrpages; i++) {
  410. page = sg_page(&sg[i]);
  411. if (!page)
  412. break;
  413. put_page(page);
  414. }
  415. kfree(sg);
  416. }
  417. /* Callers must take the port->outvq_lock */
  418. static void reclaim_consumed_buffers(struct port *port)
  419. {
  420. struct buffer_token *tok;
  421. unsigned int len;
  422. if (!port->portdev) {
  423. /* Device has been unplugged. vqs are already gone. */
  424. return;
  425. }
  426. while ((tok = virtqueue_get_buf(port->out_vq, &len))) {
  427. if (tok->sgpages)
  428. reclaim_sg_pages(tok->u.sg, tok->sgpages);
  429. else
  430. kfree(tok->u.buf);
  431. kfree(tok);
  432. port->outvq_full = false;
  433. }
  434. }
  435. static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
  436. int nents, size_t in_count,
  437. struct buffer_token *tok, bool nonblock)
  438. {
  439. struct virtqueue *out_vq;
  440. ssize_t ret;
  441. unsigned long flags;
  442. unsigned int len;
  443. out_vq = port->out_vq;
  444. spin_lock_irqsave(&port->outvq_lock, flags);
  445. reclaim_consumed_buffers(port);
  446. ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC);
  447. /* Tell Host to go! */
  448. virtqueue_kick(out_vq);
  449. if (ret < 0) {
  450. in_count = 0;
  451. goto done;
  452. }
  453. if (ret == 0)
  454. port->outvq_full = true;
  455. if (nonblock)
  456. goto done;
  457. /*
  458. * Wait till the host acknowledges it pushed out the data we
  459. * sent. This is done for data from the hvc_console; the tty
  460. * operations are performed with spinlocks held so we can't
  461. * sleep here. An alternative would be to copy the data to a
  462. * buffer and relax the spinning requirement. The downside is
  463. * we need to kmalloc a GFP_ATOMIC buffer each time the
  464. * console driver writes something out.
  465. */
  466. while (!virtqueue_get_buf(out_vq, &len))
  467. cpu_relax();
  468. done:
  469. spin_unlock_irqrestore(&port->outvq_lock, flags);
  470. port->stats.bytes_sent += in_count;
  471. /*
  472. * We're expected to return the amount of data we wrote -- all
  473. * of it
  474. */
  475. return in_count;
  476. }
  477. static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
  478. bool nonblock)
  479. {
  480. struct scatterlist sg[1];
  481. struct buffer_token *tok;
  482. tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
  483. if (!tok)
  484. return -ENOMEM;
  485. tok->sgpages = 0;
  486. tok->u.buf = in_buf;
  487. sg_init_one(sg, in_buf, in_count);
  488. return __send_to_port(port, sg, 1, in_count, tok, nonblock);
  489. }
  490. static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
  491. size_t in_count, bool nonblock)
  492. {
  493. struct buffer_token *tok;
  494. tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
  495. if (!tok)
  496. return -ENOMEM;
  497. tok->sgpages = nents;
  498. tok->u.sg = sg;
  499. return __send_to_port(port, sg, nents, in_count, tok, nonblock);
  500. }
  501. /*
  502. * Give out the data that's requested from the buffer that we have
  503. * queued up.
  504. */
  505. static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
  506. bool to_user)
  507. {
  508. struct port_buffer *buf;
  509. unsigned long flags;
  510. if (!out_count || !port_has_data(port))
  511. return 0;
  512. buf = port->inbuf;
  513. out_count = min(out_count, buf->len - buf->offset);
  514. if (to_user) {
  515. ssize_t ret;
  516. ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
  517. if (ret)
  518. return -EFAULT;
  519. } else {
  520. memcpy(out_buf, buf->buf + buf->offset, out_count);
  521. }
  522. buf->offset += out_count;
  523. if (buf->offset == buf->len) {
  524. /*
  525. * We're done using all the data in this buffer.
  526. * Re-queue so that the Host can send us more data.
  527. */
  528. spin_lock_irqsave(&port->inbuf_lock, flags);
  529. port->inbuf = NULL;
  530. if (add_inbuf(port->in_vq, buf) < 0)
  531. dev_warn(port->dev, "failed add_buf\n");
  532. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  533. }
  534. /* Return the number of bytes actually copied */
  535. return out_count;
  536. }
  537. /* The condition that must be true for polling to end */
  538. static bool will_read_block(struct port *port)
  539. {
  540. if (!port->guest_connected) {
  541. /* Port got hot-unplugged. Let's exit. */
  542. return false;
  543. }
  544. return !port_has_data(port) && port->host_connected;
  545. }
  546. static bool will_write_block(struct port *port)
  547. {
  548. bool ret;
  549. if (!port->guest_connected) {
  550. /* Port got hot-unplugged. Let's exit. */
  551. return false;
  552. }
  553. if (!port->host_connected)
  554. return true;
  555. spin_lock_irq(&port->outvq_lock);
  556. /*
  557. * Check if the Host has consumed any buffers since we last
  558. * sent data (this is only applicable for nonblocking ports).
  559. */
  560. reclaim_consumed_buffers(port);
  561. ret = port->outvq_full;
  562. spin_unlock_irq(&port->outvq_lock);
  563. return ret;
  564. }
  565. static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
  566. size_t count, loff_t *offp)
  567. {
  568. struct port *port;
  569. ssize_t ret;
  570. port = filp->private_data;
  571. if (!port_has_data(port)) {
  572. /*
  573. * If nothing's connected on the host just return 0 in
  574. * case of list_empty; this tells the userspace app
  575. * that there's no connection
  576. */
  577. if (!port->host_connected)
  578. return 0;
  579. if (filp->f_flags & O_NONBLOCK)
  580. return -EAGAIN;
  581. ret = wait_event_freezable(port->waitqueue,
  582. !will_read_block(port));
  583. if (ret < 0)
  584. return ret;
  585. }
  586. /* Port got hot-unplugged. */
  587. if (!port->guest_connected)
  588. return -ENODEV;
  589. /*
  590. * We could've received a disconnection message while we were
  591. * waiting for more data.
  592. *
  593. * This check is not clubbed in the if() statement above as we
  594. * might receive some data as well as the host could get
  595. * disconnected after we got woken up from our wait. So we
  596. * really want to give off whatever data we have and only then
  597. * check for host_connected.
  598. */
  599. if (!port_has_data(port) && !port->host_connected)
  600. return 0;
  601. return fill_readbuf(port, ubuf, count, true);
  602. }
  603. static int wait_port_writable(struct port *port, bool nonblock)
  604. {
  605. int ret;
  606. if (will_write_block(port)) {
  607. if (nonblock)
  608. return -EAGAIN;
  609. ret = wait_event_freezable(port->waitqueue,
  610. !will_write_block(port));
  611. if (ret < 0)
  612. return ret;
  613. }
  614. /* Port got hot-unplugged. */
  615. if (!port->guest_connected)
  616. return -ENODEV;
  617. return 0;
  618. }
  619. static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
  620. size_t count, loff_t *offp)
  621. {
  622. struct port *port;
  623. char *buf;
  624. ssize_t ret;
  625. bool nonblock;
  626. /* Userspace could be out to fool us */
  627. if (!count)
  628. return 0;
  629. port = filp->private_data;
  630. nonblock = filp->f_flags & O_NONBLOCK;
  631. ret = wait_port_writable(port, nonblock);
  632. if (ret < 0)
  633. return ret;
  634. count = min((size_t)(32 * 1024), count);
  635. buf = kmalloc(count, GFP_KERNEL);
  636. if (!buf)
  637. return -ENOMEM;
  638. ret = copy_from_user(buf, ubuf, count);
  639. if (ret) {
  640. ret = -EFAULT;
  641. goto free_buf;
  642. }
  643. /*
  644. * We now ask send_buf() to not spin for generic ports -- we
  645. * can re-use the same code path that non-blocking file
  646. * descriptors take for blocking file descriptors since the
  647. * wait is already done and we're certain the write will go
  648. * through to the host.
  649. */
  650. nonblock = true;
  651. ret = send_buf(port, buf, count, nonblock);
  652. if (nonblock && ret > 0)
  653. goto out;
  654. free_buf:
  655. kfree(buf);
  656. out:
  657. return ret;
  658. }
  659. struct sg_list {
  660. unsigned int n;
  661. unsigned int size;
  662. size_t len;
  663. struct scatterlist *sg;
  664. };
  665. static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  666. struct splice_desc *sd)
  667. {
  668. struct sg_list *sgl = sd->u.data;
  669. unsigned int offset, len;
  670. if (sgl->n == sgl->size)
  671. return 0;
  672. /* Try lock this page */
  673. if (buf->ops->steal(pipe, buf) == 0) {
  674. /* Get reference and unlock page for moving */
  675. get_page(buf->page);
  676. unlock_page(buf->page);
  677. len = min(buf->len, sd->len);
  678. sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
  679. } else {
  680. /* Failback to copying a page */
  681. struct page *page = alloc_page(GFP_KERNEL);
  682. char *src = buf->ops->map(pipe, buf, 1);
  683. char *dst;
  684. if (!page)
  685. return -ENOMEM;
  686. dst = kmap(page);
  687. offset = sd->pos & ~PAGE_MASK;
  688. len = sd->len;
  689. if (len + offset > PAGE_SIZE)
  690. len = PAGE_SIZE - offset;
  691. memcpy(dst + offset, src + buf->offset, len);
  692. kunmap(page);
  693. buf->ops->unmap(pipe, buf, src);
  694. sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
  695. }
  696. sgl->n++;
  697. sgl->len += len;
  698. return len;
  699. }
  700. /* Faster zero-copy write by splicing */
  701. static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
  702. struct file *filp, loff_t *ppos,
  703. size_t len, unsigned int flags)
  704. {
  705. struct port *port = filp->private_data;
  706. struct sg_list sgl;
  707. ssize_t ret;
  708. struct splice_desc sd = {
  709. .total_len = len,
  710. .flags = flags,
  711. .pos = *ppos,
  712. .u.data = &sgl,
  713. };
  714. ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
  715. if (ret < 0)
  716. return ret;
  717. sgl.n = 0;
  718. sgl.len = 0;
  719. sgl.size = pipe->nrbufs;
  720. sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL);
  721. if (unlikely(!sgl.sg))
  722. return -ENOMEM;
  723. sg_init_table(sgl.sg, sgl.size);
  724. ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
  725. if (likely(ret > 0))
  726. ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true);
  727. return ret;
  728. }
  729. static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
  730. {
  731. struct port *port;
  732. unsigned int ret;
  733. port = filp->private_data;
  734. poll_wait(filp, &port->waitqueue, wait);
  735. if (!port->guest_connected) {
  736. /* Port got unplugged */
  737. return POLLHUP;
  738. }
  739. ret = 0;
  740. if (!will_read_block(port))
  741. ret |= POLLIN | POLLRDNORM;
  742. if (!will_write_block(port))
  743. ret |= POLLOUT;
  744. if (!port->host_connected)
  745. ret |= POLLHUP;
  746. return ret;
  747. }
  748. static void remove_port(struct kref *kref);
  749. static int port_fops_release(struct inode *inode, struct file *filp)
  750. {
  751. struct port *port;
  752. port = filp->private_data;
  753. /* Notify host of port being closed */
  754. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  755. spin_lock_irq(&port->inbuf_lock);
  756. port->guest_connected = false;
  757. discard_port_data(port);
  758. spin_unlock_irq(&port->inbuf_lock);
  759. spin_lock_irq(&port->outvq_lock);
  760. reclaim_consumed_buffers(port);
  761. spin_unlock_irq(&port->outvq_lock);
  762. /*
  763. * Locks aren't necessary here as a port can't be opened after
  764. * unplug, and if a port isn't unplugged, a kref would already
  765. * exist for the port. Plus, taking ports_lock here would
  766. * create a dependency on other locks taken by functions
  767. * inside remove_port if we're the last holder of the port,
  768. * creating many problems.
  769. */
  770. kref_put(&port->kref, remove_port);
  771. return 0;
  772. }
  773. static int port_fops_open(struct inode *inode, struct file *filp)
  774. {
  775. struct cdev *cdev = inode->i_cdev;
  776. struct port *port;
  777. int ret;
  778. port = find_port_by_devt(cdev->dev);
  779. filp->private_data = port;
  780. /* Prevent against a port getting hot-unplugged at the same time */
  781. spin_lock_irq(&port->portdev->ports_lock);
  782. kref_get(&port->kref);
  783. spin_unlock_irq(&port->portdev->ports_lock);
  784. /*
  785. * Don't allow opening of console port devices -- that's done
  786. * via /dev/hvc
  787. */
  788. if (is_console_port(port)) {
  789. ret = -ENXIO;
  790. goto out;
  791. }
  792. /* Allow only one process to open a particular port at a time */
  793. spin_lock_irq(&port->inbuf_lock);
  794. if (port->guest_connected) {
  795. spin_unlock_irq(&port->inbuf_lock);
  796. ret = -EMFILE;
  797. goto out;
  798. }
  799. port->guest_connected = true;
  800. spin_unlock_irq(&port->inbuf_lock);
  801. spin_lock_irq(&port->outvq_lock);
  802. /*
  803. * There might be a chance that we missed reclaiming a few
  804. * buffers in the window of the port getting previously closed
  805. * and opening now.
  806. */
  807. reclaim_consumed_buffers(port);
  808. spin_unlock_irq(&port->outvq_lock);
  809. nonseekable_open(inode, filp);
  810. /* Notify host of port being opened */
  811. send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
  812. return 0;
  813. out:
  814. kref_put(&port->kref, remove_port);
  815. return ret;
  816. }
  817. static int port_fops_fasync(int fd, struct file *filp, int mode)
  818. {
  819. struct port *port;
  820. port = filp->private_data;
  821. return fasync_helper(fd, filp, mode, &port->async_queue);
  822. }
  823. /*
  824. * The file operations that we support: programs in the guest can open
  825. * a console device, read from it, write to it, poll for data and
  826. * close it. The devices are at
  827. * /dev/vport<device number>p<port number>
  828. */
  829. static const struct file_operations port_fops = {
  830. .owner = THIS_MODULE,
  831. .open = port_fops_open,
  832. .read = port_fops_read,
  833. .write = port_fops_write,
  834. .splice_write = port_fops_splice_write,
  835. .poll = port_fops_poll,
  836. .release = port_fops_release,
  837. .fasync = port_fops_fasync,
  838. .llseek = no_llseek,
  839. };
  840. /*
  841. * The put_chars() callback is pretty straightforward.
  842. *
  843. * We turn the characters into a scatter-gather list, add it to the
  844. * output queue and then kick the Host. Then we sit here waiting for
  845. * it to finish: inefficient in theory, but in practice
  846. * implementations will do it immediately (lguest's Launcher does).
  847. */
  848. static int put_chars(u32 vtermno, const char *buf, int count)
  849. {
  850. struct port *port;
  851. if (unlikely(early_put_chars))
  852. return early_put_chars(vtermno, buf, count);
  853. port = find_port_by_vtermno(vtermno);
  854. if (!port)
  855. return -EPIPE;
  856. return send_buf(port, (void *)buf, count, false);
  857. }
  858. /*
  859. * get_chars() is the callback from the hvc_console infrastructure
  860. * when an interrupt is received.
  861. *
  862. * We call out to fill_readbuf that gets us the required data from the
  863. * buffers that are queued up.
  864. */
  865. static int get_chars(u32 vtermno, char *buf, int count)
  866. {
  867. struct port *port;
  868. /* If we've not set up the port yet, we have no input to give. */
  869. if (unlikely(early_put_chars))
  870. return 0;
  871. port = find_port_by_vtermno(vtermno);
  872. if (!port)
  873. return -EPIPE;
  874. /* If we don't have an input queue yet, we can't get input. */
  875. BUG_ON(!port->in_vq);
  876. return fill_readbuf(port, buf, count, false);
  877. }
  878. static void resize_console(struct port *port)
  879. {
  880. struct virtio_device *vdev;
  881. /* The port could have been hot-unplugged */
  882. if (!port || !is_console_port(port))
  883. return;
  884. vdev = port->portdev->vdev;
  885. if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
  886. hvc_resize(port->cons.hvc, port->cons.ws);
  887. }
  888. /* We set the configuration at this point, since we now have a tty */
  889. static int notifier_add_vio(struct hvc_struct *hp, int data)
  890. {
  891. struct port *port;
  892. port = find_port_by_vtermno(hp->vtermno);
  893. if (!port)
  894. return -EINVAL;
  895. hp->irq_requested = 1;
  896. resize_console(port);
  897. return 0;
  898. }
  899. static void notifier_del_vio(struct hvc_struct *hp, int data)
  900. {
  901. hp->irq_requested = 0;
  902. }
  903. /* The operations for console ports. */
  904. static const struct hv_ops hv_ops = {
  905. .get_chars = get_chars,
  906. .put_chars = put_chars,
  907. .notifier_add = notifier_add_vio,
  908. .notifier_del = notifier_del_vio,
  909. .notifier_hangup = notifier_del_vio,
  910. };
  911. /*
  912. * Console drivers are initialized very early so boot messages can go
  913. * out, so we do things slightly differently from the generic virtio
  914. * initialization of the net and block drivers.
  915. *
  916. * At this stage, the console is output-only. It's too early to set
  917. * up a virtqueue, so we let the drivers do some boutique early-output
  918. * thing.
  919. */
  920. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  921. {
  922. early_put_chars = put_chars;
  923. return hvc_instantiate(0, 0, &hv_ops);
  924. }
  925. int init_port_console(struct port *port)
  926. {
  927. int ret;
  928. /*
  929. * The Host's telling us this port is a console port. Hook it
  930. * up with an hvc console.
  931. *
  932. * To set up and manage our virtual console, we call
  933. * hvc_alloc().
  934. *
  935. * The first argument of hvc_alloc() is the virtual console
  936. * number. The second argument is the parameter for the
  937. * notification mechanism (like irq number). We currently
  938. * leave this as zero, virtqueues have implicit notifications.
  939. *
  940. * The third argument is a "struct hv_ops" containing the
  941. * put_chars() get_chars(), notifier_add() and notifier_del()
  942. * pointers. The final argument is the output buffer size: we
  943. * can do any size, so we put PAGE_SIZE here.
  944. */
  945. port->cons.vtermno = pdrvdata.next_vtermno;
  946. port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
  947. if (IS_ERR(port->cons.hvc)) {
  948. ret = PTR_ERR(port->cons.hvc);
  949. dev_err(port->dev,
  950. "error %d allocating hvc for port\n", ret);
  951. port->cons.hvc = NULL;
  952. return ret;
  953. }
  954. spin_lock_irq(&pdrvdata_lock);
  955. pdrvdata.next_vtermno++;
  956. list_add_tail(&port->cons.list, &pdrvdata.consoles);
  957. spin_unlock_irq(&pdrvdata_lock);
  958. port->guest_connected = true;
  959. /*
  960. * Start using the new console output if this is the first
  961. * console to come up.
  962. */
  963. if (early_put_chars)
  964. early_put_chars = NULL;
  965. /* Notify host of port being opened */
  966. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  967. return 0;
  968. }
  969. static ssize_t show_port_name(struct device *dev,
  970. struct device_attribute *attr, char *buffer)
  971. {
  972. struct port *port;
  973. port = dev_get_drvdata(dev);
  974. return sprintf(buffer, "%s\n", port->name);
  975. }
  976. static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
  977. static struct attribute *port_sysfs_entries[] = {
  978. &dev_attr_name.attr,
  979. NULL
  980. };
  981. static struct attribute_group port_attribute_group = {
  982. .name = NULL, /* put in device directory */
  983. .attrs = port_sysfs_entries,
  984. };
  985. static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
  986. size_t count, loff_t *offp)
  987. {
  988. struct port *port;
  989. char *buf;
  990. ssize_t ret, out_offset, out_count;
  991. out_count = 1024;
  992. buf = kmalloc(out_count, GFP_KERNEL);
  993. if (!buf)
  994. return -ENOMEM;
  995. port = filp->private_data;
  996. out_offset = 0;
  997. out_offset += snprintf(buf + out_offset, out_count,
  998. "name: %s\n", port->name ? port->name : "");
  999. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1000. "guest_connected: %d\n", port->guest_connected);
  1001. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1002. "host_connected: %d\n", port->host_connected);
  1003. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1004. "outvq_full: %d\n", port->outvq_full);
  1005. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1006. "bytes_sent: %lu\n", port->stats.bytes_sent);
  1007. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1008. "bytes_received: %lu\n",
  1009. port->stats.bytes_received);
  1010. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1011. "bytes_discarded: %lu\n",
  1012. port->stats.bytes_discarded);
  1013. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1014. "is_console: %s\n",
  1015. is_console_port(port) ? "yes" : "no");
  1016. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  1017. "console_vtermno: %u\n", port->cons.vtermno);
  1018. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  1019. kfree(buf);
  1020. return ret;
  1021. }
  1022. static const struct file_operations port_debugfs_ops = {
  1023. .owner = THIS_MODULE,
  1024. .open = simple_open,
  1025. .read = debugfs_read,
  1026. };
  1027. static void set_console_size(struct port *port, u16 rows, u16 cols)
  1028. {
  1029. if (!port || !is_console_port(port))
  1030. return;
  1031. port->cons.ws.ws_row = rows;
  1032. port->cons.ws.ws_col = cols;
  1033. }
  1034. static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
  1035. {
  1036. struct port_buffer *buf;
  1037. unsigned int nr_added_bufs;
  1038. int ret;
  1039. nr_added_bufs = 0;
  1040. do {
  1041. buf = alloc_buf(PAGE_SIZE);
  1042. if (!buf)
  1043. break;
  1044. spin_lock_irq(lock);
  1045. ret = add_inbuf(vq, buf);
  1046. if (ret < 0) {
  1047. spin_unlock_irq(lock);
  1048. free_buf(buf);
  1049. break;
  1050. }
  1051. nr_added_bufs++;
  1052. spin_unlock_irq(lock);
  1053. } while (ret > 0);
  1054. return nr_added_bufs;
  1055. }
  1056. static void send_sigio_to_port(struct port *port)
  1057. {
  1058. if (port->async_queue && port->guest_connected)
  1059. kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
  1060. }
  1061. static int add_port(struct ports_device *portdev, u32 id)
  1062. {
  1063. char debugfs_name[16];
  1064. struct port *port;
  1065. struct port_buffer *buf;
  1066. dev_t devt;
  1067. unsigned int nr_added_bufs;
  1068. int err;
  1069. port = kmalloc(sizeof(*port), GFP_KERNEL);
  1070. if (!port) {
  1071. err = -ENOMEM;
  1072. goto fail;
  1073. }
  1074. kref_init(&port->kref);
  1075. port->portdev = portdev;
  1076. port->id = id;
  1077. port->name = NULL;
  1078. port->inbuf = NULL;
  1079. port->cons.hvc = NULL;
  1080. port->async_queue = NULL;
  1081. port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
  1082. port->host_connected = port->guest_connected = false;
  1083. port->stats = (struct port_stats) { 0 };
  1084. port->outvq_full = false;
  1085. port->in_vq = portdev->in_vqs[port->id];
  1086. port->out_vq = portdev->out_vqs[port->id];
  1087. port->cdev = cdev_alloc();
  1088. if (!port->cdev) {
  1089. dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
  1090. err = -ENOMEM;
  1091. goto free_port;
  1092. }
  1093. port->cdev->ops = &port_fops;
  1094. devt = MKDEV(portdev->chr_major, id);
  1095. err = cdev_add(port->cdev, devt, 1);
  1096. if (err < 0) {
  1097. dev_err(&port->portdev->vdev->dev,
  1098. "Error %d adding cdev for port %u\n", err, id);
  1099. goto free_cdev;
  1100. }
  1101. port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
  1102. devt, port, "vport%up%u",
  1103. port->portdev->drv_index, id);
  1104. if (IS_ERR(port->dev)) {
  1105. err = PTR_ERR(port->dev);
  1106. dev_err(&port->portdev->vdev->dev,
  1107. "Error %d creating device for port %u\n",
  1108. err, id);
  1109. goto free_cdev;
  1110. }
  1111. spin_lock_init(&port->inbuf_lock);
  1112. spin_lock_init(&port->outvq_lock);
  1113. init_waitqueue_head(&port->waitqueue);
  1114. /* Fill the in_vq with buffers so the host can send us data. */
  1115. nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
  1116. if (!nr_added_bufs) {
  1117. dev_err(port->dev, "Error allocating inbufs\n");
  1118. err = -ENOMEM;
  1119. goto free_device;
  1120. }
  1121. /*
  1122. * If we're not using multiport support, this has to be a console port
  1123. */
  1124. if (!use_multiport(port->portdev)) {
  1125. err = init_port_console(port);
  1126. if (err)
  1127. goto free_inbufs;
  1128. }
  1129. spin_lock_irq(&portdev->ports_lock);
  1130. list_add_tail(&port->list, &port->portdev->ports);
  1131. spin_unlock_irq(&portdev->ports_lock);
  1132. /*
  1133. * Tell the Host we're set so that it can send us various
  1134. * configuration parameters for this port (eg, port name,
  1135. * caching, whether this is a console port, etc.)
  1136. */
  1137. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1138. if (pdrvdata.debugfs_dir) {
  1139. /*
  1140. * Finally, create the debugfs file that we can use to
  1141. * inspect a port's state at any time
  1142. */
  1143. sprintf(debugfs_name, "vport%up%u",
  1144. port->portdev->drv_index, id);
  1145. port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
  1146. pdrvdata.debugfs_dir,
  1147. port,
  1148. &port_debugfs_ops);
  1149. }
  1150. return 0;
  1151. free_inbufs:
  1152. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1153. free_buf(buf);
  1154. free_device:
  1155. device_destroy(pdrvdata.class, port->dev->devt);
  1156. free_cdev:
  1157. cdev_del(port->cdev);
  1158. free_port:
  1159. kfree(port);
  1160. fail:
  1161. /* The host might want to notify management sw about port add failure */
  1162. __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
  1163. return err;
  1164. }
  1165. /* No users remain, remove all port-specific data. */
  1166. static void remove_port(struct kref *kref)
  1167. {
  1168. struct port *port;
  1169. port = container_of(kref, struct port, kref);
  1170. sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
  1171. device_destroy(pdrvdata.class, port->dev->devt);
  1172. cdev_del(port->cdev);
  1173. kfree(port->name);
  1174. debugfs_remove(port->debugfs_file);
  1175. kfree(port);
  1176. }
  1177. static void remove_port_data(struct port *port)
  1178. {
  1179. struct port_buffer *buf;
  1180. /* Remove unused data this port might have received. */
  1181. discard_port_data(port);
  1182. reclaim_consumed_buffers(port);
  1183. /* Remove buffers we queued up for the Host to send us data in. */
  1184. while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
  1185. free_buf(buf);
  1186. }
  1187. /*
  1188. * Port got unplugged. Remove port from portdev's list and drop the
  1189. * kref reference. If no userspace has this port opened, it will
  1190. * result in immediate removal the port.
  1191. */
  1192. static void unplug_port(struct port *port)
  1193. {
  1194. spin_lock_irq(&port->portdev->ports_lock);
  1195. list_del(&port->list);
  1196. spin_unlock_irq(&port->portdev->ports_lock);
  1197. if (port->guest_connected) {
  1198. port->guest_connected = false;
  1199. port->host_connected = false;
  1200. wake_up_interruptible(&port->waitqueue);
  1201. /* Let the app know the port is going down. */
  1202. send_sigio_to_port(port);
  1203. }
  1204. if (is_console_port(port)) {
  1205. spin_lock_irq(&pdrvdata_lock);
  1206. list_del(&port->cons.list);
  1207. spin_unlock_irq(&pdrvdata_lock);
  1208. hvc_remove(port->cons.hvc);
  1209. }
  1210. remove_port_data(port);
  1211. /*
  1212. * We should just assume the device itself has gone off --
  1213. * else a close on an open port later will try to send out a
  1214. * control message.
  1215. */
  1216. port->portdev = NULL;
  1217. /*
  1218. * Locks around here are not necessary - a port can't be
  1219. * opened after we removed the port struct from ports_list
  1220. * above.
  1221. */
  1222. kref_put(&port->kref, remove_port);
  1223. }
  1224. /* Any private messages that the Host and Guest want to share */
  1225. static void handle_control_message(struct ports_device *portdev,
  1226. struct port_buffer *buf)
  1227. {
  1228. struct virtio_console_control *cpkt;
  1229. struct port *port;
  1230. size_t name_size;
  1231. int err;
  1232. cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
  1233. port = find_port_by_id(portdev, cpkt->id);
  1234. if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
  1235. /* No valid header at start of buffer. Drop it. */
  1236. dev_dbg(&portdev->vdev->dev,
  1237. "Invalid index %u in control packet\n", cpkt->id);
  1238. return;
  1239. }
  1240. switch (cpkt->event) {
  1241. case VIRTIO_CONSOLE_PORT_ADD:
  1242. if (port) {
  1243. dev_dbg(&portdev->vdev->dev,
  1244. "Port %u already added\n", port->id);
  1245. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1246. break;
  1247. }
  1248. if (cpkt->id >= portdev->config.max_nr_ports) {
  1249. dev_warn(&portdev->vdev->dev,
  1250. "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
  1251. cpkt->id, portdev->config.max_nr_ports - 1);
  1252. break;
  1253. }
  1254. add_port(portdev, cpkt->id);
  1255. break;
  1256. case VIRTIO_CONSOLE_PORT_REMOVE:
  1257. unplug_port(port);
  1258. break;
  1259. case VIRTIO_CONSOLE_CONSOLE_PORT:
  1260. if (!cpkt->value)
  1261. break;
  1262. if (is_console_port(port))
  1263. break;
  1264. init_port_console(port);
  1265. complete(&early_console_added);
  1266. /*
  1267. * Could remove the port here in case init fails - but
  1268. * have to notify the host first.
  1269. */
  1270. break;
  1271. case VIRTIO_CONSOLE_RESIZE: {
  1272. struct {
  1273. __u16 rows;
  1274. __u16 cols;
  1275. } size;
  1276. if (!is_console_port(port))
  1277. break;
  1278. memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
  1279. sizeof(size));
  1280. set_console_size(port, size.rows, size.cols);
  1281. port->cons.hvc->irq_requested = 1;
  1282. resize_console(port);
  1283. break;
  1284. }
  1285. case VIRTIO_CONSOLE_PORT_OPEN:
  1286. port->host_connected = cpkt->value;
  1287. wake_up_interruptible(&port->waitqueue);
  1288. /*
  1289. * If the host port got closed and the host had any
  1290. * unconsumed buffers, we'll be able to reclaim them
  1291. * now.
  1292. */
  1293. spin_lock_irq(&port->outvq_lock);
  1294. reclaim_consumed_buffers(port);
  1295. spin_unlock_irq(&port->outvq_lock);
  1296. /*
  1297. * If the guest is connected, it'll be interested in
  1298. * knowing the host connection state changed.
  1299. */
  1300. send_sigio_to_port(port);
  1301. break;
  1302. case VIRTIO_CONSOLE_PORT_NAME:
  1303. /*
  1304. * If we woke up after hibernation, we can get this
  1305. * again. Skip it in that case.
  1306. */
  1307. if (port->name)
  1308. break;
  1309. /*
  1310. * Skip the size of the header and the cpkt to get the size
  1311. * of the name that was sent
  1312. */
  1313. name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
  1314. port->name = kmalloc(name_size, GFP_KERNEL);
  1315. if (!port->name) {
  1316. dev_err(port->dev,
  1317. "Not enough space to store port name\n");
  1318. break;
  1319. }
  1320. strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
  1321. name_size - 1);
  1322. port->name[name_size - 1] = 0;
  1323. /*
  1324. * Since we only have one sysfs attribute, 'name',
  1325. * create it only if we have a name for the port.
  1326. */
  1327. err = sysfs_create_group(&port->dev->kobj,
  1328. &port_attribute_group);
  1329. if (err) {
  1330. dev_err(port->dev,
  1331. "Error %d creating sysfs device attributes\n",
  1332. err);
  1333. } else {
  1334. /*
  1335. * Generate a udev event so that appropriate
  1336. * symlinks can be created based on udev
  1337. * rules.
  1338. */
  1339. kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
  1340. }
  1341. break;
  1342. }
  1343. }
  1344. static void control_work_handler(struct work_struct *work)
  1345. {
  1346. struct ports_device *portdev;
  1347. struct virtqueue *vq;
  1348. struct port_buffer *buf;
  1349. unsigned int len;
  1350. portdev = container_of(work, struct ports_device, control_work);
  1351. vq = portdev->c_ivq;
  1352. spin_lock(&portdev->cvq_lock);
  1353. while ((buf = virtqueue_get_buf(vq, &len))) {
  1354. spin_unlock(&portdev->cvq_lock);
  1355. buf->len = len;
  1356. buf->offset = 0;
  1357. handle_control_message(portdev, buf);
  1358. spin_lock(&portdev->cvq_lock);
  1359. if (add_inbuf(portdev->c_ivq, buf) < 0) {
  1360. dev_warn(&portdev->vdev->dev,
  1361. "Error adding buffer to queue\n");
  1362. free_buf(buf);
  1363. }
  1364. }
  1365. spin_unlock(&portdev->cvq_lock);
  1366. }
  1367. static void out_intr(struct virtqueue *vq)
  1368. {
  1369. struct port *port;
  1370. port = find_port_by_vq(vq->vdev->priv, vq);
  1371. if (!port)
  1372. return;
  1373. wake_up_interruptible(&port->waitqueue);
  1374. }
  1375. static void in_intr(struct virtqueue *vq)
  1376. {
  1377. struct port *port;
  1378. unsigned long flags;
  1379. port = find_port_by_vq(vq->vdev->priv, vq);
  1380. if (!port)
  1381. return;
  1382. spin_lock_irqsave(&port->inbuf_lock, flags);
  1383. port->inbuf = get_inbuf(port);
  1384. /*
  1385. * Don't queue up data when port is closed. This condition
  1386. * can be reached when a console port is not yet connected (no
  1387. * tty is spawned) and the host sends out data to console
  1388. * ports. For generic serial ports, the host won't
  1389. * (shouldn't) send data till the guest is connected.
  1390. */
  1391. if (!port->guest_connected)
  1392. discard_port_data(port);
  1393. spin_unlock_irqrestore(&port->inbuf_lock, flags);
  1394. wake_up_interruptible(&port->waitqueue);
  1395. /* Send a SIGIO indicating new data in case the process asked for it */
  1396. send_sigio_to_port(port);
  1397. if (is_console_port(port) && hvc_poll(port->cons.hvc))
  1398. hvc_kick();
  1399. }
  1400. static void control_intr(struct virtqueue *vq)
  1401. {
  1402. struct ports_device *portdev;
  1403. portdev = vq->vdev->priv;
  1404. schedule_work(&portdev->control_work);
  1405. }
  1406. static void config_intr(struct virtio_device *vdev)
  1407. {
  1408. struct ports_device *portdev;
  1409. portdev = vdev->priv;
  1410. if (!use_multiport(portdev)) {
  1411. struct port *port;
  1412. u16 rows, cols;
  1413. vdev->config->get(vdev,
  1414. offsetof(struct virtio_console_config, cols),
  1415. &cols, sizeof(u16));
  1416. vdev->config->get(vdev,
  1417. offsetof(struct virtio_console_config, rows),
  1418. &rows, sizeof(u16));
  1419. port = find_port_by_id(portdev, 0);
  1420. set_console_size(port, rows, cols);
  1421. /*
  1422. * We'll use this way of resizing only for legacy
  1423. * support. For newer userspace
  1424. * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
  1425. * to indicate console size changes so that it can be
  1426. * done per-port.
  1427. */
  1428. resize_console(port);
  1429. }
  1430. }
  1431. static int init_vqs(struct ports_device *portdev)
  1432. {
  1433. vq_callback_t **io_callbacks;
  1434. char **io_names;
  1435. struct virtqueue **vqs;
  1436. u32 i, j, nr_ports, nr_queues;
  1437. int err;
  1438. nr_ports = portdev->config.max_nr_ports;
  1439. nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
  1440. vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
  1441. io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
  1442. io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
  1443. portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1444. GFP_KERNEL);
  1445. portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
  1446. GFP_KERNEL);
  1447. if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
  1448. !portdev->out_vqs) {
  1449. err = -ENOMEM;
  1450. goto free;
  1451. }
  1452. /*
  1453. * For backward compat (newer host but older guest), the host
  1454. * spawns a console port first and also inits the vqs for port
  1455. * 0 before others.
  1456. */
  1457. j = 0;
  1458. io_callbacks[j] = in_intr;
  1459. io_callbacks[j + 1] = out_intr;
  1460. io_names[j] = "input";
  1461. io_names[j + 1] = "output";
  1462. j += 2;
  1463. if (use_multiport(portdev)) {
  1464. io_callbacks[j] = control_intr;
  1465. io_callbacks[j + 1] = NULL;
  1466. io_names[j] = "control-i";
  1467. io_names[j + 1] = "control-o";
  1468. for (i = 1; i < nr_ports; i++) {
  1469. j += 2;
  1470. io_callbacks[j] = in_intr;
  1471. io_callbacks[j + 1] = out_intr;
  1472. io_names[j] = "input";
  1473. io_names[j + 1] = "output";
  1474. }
  1475. }
  1476. /* Find the queues. */
  1477. err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
  1478. io_callbacks,
  1479. (const char **)io_names);
  1480. if (err)
  1481. goto free;
  1482. j = 0;
  1483. portdev->in_vqs[0] = vqs[0];
  1484. portdev->out_vqs[0] = vqs[1];
  1485. j += 2;
  1486. if (use_multiport(portdev)) {
  1487. portdev->c_ivq = vqs[j];
  1488. portdev->c_ovq = vqs[j + 1];
  1489. for (i = 1; i < nr_ports; i++) {
  1490. j += 2;
  1491. portdev->in_vqs[i] = vqs[j];
  1492. portdev->out_vqs[i] = vqs[j + 1];
  1493. }
  1494. }
  1495. kfree(io_names);
  1496. kfree(io_callbacks);
  1497. kfree(vqs);
  1498. return 0;
  1499. free:
  1500. kfree(portdev->out_vqs);
  1501. kfree(portdev->in_vqs);
  1502. kfree(io_names);
  1503. kfree(io_callbacks);
  1504. kfree(vqs);
  1505. return err;
  1506. }
  1507. static const struct file_operations portdev_fops = {
  1508. .owner = THIS_MODULE,
  1509. };
  1510. static void remove_vqs(struct ports_device *portdev)
  1511. {
  1512. portdev->vdev->config->del_vqs(portdev->vdev);
  1513. kfree(portdev->in_vqs);
  1514. kfree(portdev->out_vqs);
  1515. }
  1516. static void remove_controlq_data(struct ports_device *portdev)
  1517. {
  1518. struct port_buffer *buf;
  1519. unsigned int len;
  1520. if (!use_multiport(portdev))
  1521. return;
  1522. while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
  1523. free_buf(buf);
  1524. while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
  1525. free_buf(buf);
  1526. }
  1527. /*
  1528. * Once we're further in boot, we get probed like any other virtio
  1529. * device.
  1530. *
  1531. * If the host also supports multiple console ports, we check the
  1532. * config space to see how many ports the host has spawned. We
  1533. * initialize each port found.
  1534. */
  1535. static int __devinit virtcons_probe(struct virtio_device *vdev)
  1536. {
  1537. struct ports_device *portdev;
  1538. int err;
  1539. bool multiport;
  1540. bool early = early_put_chars != NULL;
  1541. /* Ensure to read early_put_chars now */
  1542. barrier();
  1543. portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
  1544. if (!portdev) {
  1545. err = -ENOMEM;
  1546. goto fail;
  1547. }
  1548. /* Attach this portdev to this virtio_device, and vice-versa. */
  1549. portdev->vdev = vdev;
  1550. vdev->priv = portdev;
  1551. spin_lock_irq(&pdrvdata_lock);
  1552. portdev->drv_index = pdrvdata.index++;
  1553. spin_unlock_irq(&pdrvdata_lock);
  1554. portdev->chr_major = register_chrdev(0, "virtio-portsdev",
  1555. &portdev_fops);
  1556. if (portdev->chr_major < 0) {
  1557. dev_err(&vdev->dev,
  1558. "Error %d registering chrdev for device %u\n",
  1559. portdev->chr_major, portdev->drv_index);
  1560. err = portdev->chr_major;
  1561. goto free;
  1562. }
  1563. multiport = false;
  1564. portdev->config.max_nr_ports = 1;
  1565. if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
  1566. offsetof(struct virtio_console_config,
  1567. max_nr_ports),
  1568. &portdev->config.max_nr_ports) == 0)
  1569. multiport = true;
  1570. err = init_vqs(portdev);
  1571. if (err < 0) {
  1572. dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
  1573. goto free_chrdev;
  1574. }
  1575. spin_lock_init(&portdev->ports_lock);
  1576. INIT_LIST_HEAD(&portdev->ports);
  1577. if (multiport) {
  1578. unsigned int nr_added_bufs;
  1579. spin_lock_init(&portdev->cvq_lock);
  1580. INIT_WORK(&portdev->control_work, &control_work_handler);
  1581. nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1582. if (!nr_added_bufs) {
  1583. dev_err(&vdev->dev,
  1584. "Error allocating buffers for control queue\n");
  1585. err = -ENOMEM;
  1586. goto free_vqs;
  1587. }
  1588. } else {
  1589. /*
  1590. * For backward compatibility: Create a console port
  1591. * if we're running on older host.
  1592. */
  1593. add_port(portdev, 0);
  1594. }
  1595. spin_lock_irq(&pdrvdata_lock);
  1596. list_add_tail(&portdev->list, &pdrvdata.portdevs);
  1597. spin_unlock_irq(&pdrvdata_lock);
  1598. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1599. VIRTIO_CONSOLE_DEVICE_READY, 1);
  1600. /*
  1601. * If there was an early virtio console, assume that there are no
  1602. * other consoles. We need to wait until the hvc_alloc matches the
  1603. * hvc_instantiate, otherwise tty_open will complain, resulting in
  1604. * a "Warning: unable to open an initial console" boot failure.
  1605. * Without multiport this is done in add_port above. With multiport
  1606. * this might take some host<->guest communication - thus we have to
  1607. * wait.
  1608. */
  1609. if (multiport && early)
  1610. wait_for_completion(&early_console_added);
  1611. return 0;
  1612. free_vqs:
  1613. /* The host might want to notify mgmt sw about device add failure */
  1614. __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
  1615. VIRTIO_CONSOLE_DEVICE_READY, 0);
  1616. remove_vqs(portdev);
  1617. free_chrdev:
  1618. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1619. free:
  1620. kfree(portdev);
  1621. fail:
  1622. return err;
  1623. }
  1624. static void virtcons_remove(struct virtio_device *vdev)
  1625. {
  1626. struct ports_device *portdev;
  1627. struct port *port, *port2;
  1628. portdev = vdev->priv;
  1629. spin_lock_irq(&pdrvdata_lock);
  1630. list_del(&portdev->list);
  1631. spin_unlock_irq(&pdrvdata_lock);
  1632. /* Disable interrupts for vqs */
  1633. vdev->config->reset(vdev);
  1634. /* Finish up work that's lined up */
  1635. cancel_work_sync(&portdev->control_work);
  1636. list_for_each_entry_safe(port, port2, &portdev->ports, list)
  1637. unplug_port(port);
  1638. unregister_chrdev(portdev->chr_major, "virtio-portsdev");
  1639. /*
  1640. * When yanking out a device, we immediately lose the
  1641. * (device-side) queues. So there's no point in keeping the
  1642. * guest side around till we drop our final reference. This
  1643. * also means that any ports which are in an open state will
  1644. * have to just stop using the port, as the vqs are going
  1645. * away.
  1646. */
  1647. remove_controlq_data(portdev);
  1648. remove_vqs(portdev);
  1649. kfree(portdev);
  1650. }
  1651. static struct virtio_device_id id_table[] = {
  1652. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  1653. { 0 },
  1654. };
  1655. static unsigned int features[] = {
  1656. VIRTIO_CONSOLE_F_SIZE,
  1657. VIRTIO_CONSOLE_F_MULTIPORT,
  1658. };
  1659. #ifdef CONFIG_PM
  1660. static int virtcons_freeze(struct virtio_device *vdev)
  1661. {
  1662. struct ports_device *portdev;
  1663. struct port *port;
  1664. portdev = vdev->priv;
  1665. vdev->config->reset(vdev);
  1666. virtqueue_disable_cb(portdev->c_ivq);
  1667. cancel_work_sync(&portdev->control_work);
  1668. /*
  1669. * Once more: if control_work_handler() was running, it would
  1670. * enable the cb as the last step.
  1671. */
  1672. virtqueue_disable_cb(portdev->c_ivq);
  1673. remove_controlq_data(portdev);
  1674. list_for_each_entry(port, &portdev->ports, list) {
  1675. virtqueue_disable_cb(port->in_vq);
  1676. virtqueue_disable_cb(port->out_vq);
  1677. /*
  1678. * We'll ask the host later if the new invocation has
  1679. * the port opened or closed.
  1680. */
  1681. port->host_connected = false;
  1682. remove_port_data(port);
  1683. }
  1684. remove_vqs(portdev);
  1685. return 0;
  1686. }
  1687. static int virtcons_restore(struct virtio_device *vdev)
  1688. {
  1689. struct ports_device *portdev;
  1690. struct port *port;
  1691. int ret;
  1692. portdev = vdev->priv;
  1693. ret = init_vqs(portdev);
  1694. if (ret)
  1695. return ret;
  1696. if (use_multiport(portdev))
  1697. fill_queue(portdev->c_ivq, &portdev->cvq_lock);
  1698. list_for_each_entry(port, &portdev->ports, list) {
  1699. port->in_vq = portdev->in_vqs[port->id];
  1700. port->out_vq = portdev->out_vqs[port->id];
  1701. fill_queue(port->in_vq, &port->inbuf_lock);
  1702. /* Get port open/close status on the host */
  1703. send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
  1704. /*
  1705. * If a port was open at the time of suspending, we
  1706. * have to let the host know that it's still open.
  1707. */
  1708. if (port->guest_connected)
  1709. send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  1710. }
  1711. return 0;
  1712. }
  1713. #endif
  1714. static struct virtio_driver virtio_console = {
  1715. .feature_table = features,
  1716. .feature_table_size = ARRAY_SIZE(features),
  1717. .driver.name = KBUILD_MODNAME,
  1718. .driver.owner = THIS_MODULE,
  1719. .id_table = id_table,
  1720. .probe = virtcons_probe,
  1721. .remove = virtcons_remove,
  1722. .config_changed = config_intr,
  1723. #ifdef CONFIG_PM
  1724. .freeze = virtcons_freeze,
  1725. .restore = virtcons_restore,
  1726. #endif
  1727. };
  1728. static int __init init(void)
  1729. {
  1730. int err;
  1731. pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
  1732. if (IS_ERR(pdrvdata.class)) {
  1733. err = PTR_ERR(pdrvdata.class);
  1734. pr_err("Error %d creating virtio-ports class\n", err);
  1735. return err;
  1736. }
  1737. pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
  1738. if (!pdrvdata.debugfs_dir) {
  1739. pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
  1740. PTR_ERR(pdrvdata.debugfs_dir));
  1741. }
  1742. INIT_LIST_HEAD(&pdrvdata.consoles);
  1743. INIT_LIST_HEAD(&pdrvdata.portdevs);
  1744. err = register_virtio_driver(&virtio_console);
  1745. if (err < 0) {
  1746. pr_err("Error %d registering virtio driver\n", err);
  1747. goto free;
  1748. }
  1749. return 0;
  1750. free:
  1751. if (pdrvdata.debugfs_dir)
  1752. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1753. class_destroy(pdrvdata.class);
  1754. return err;
  1755. }
  1756. static void __exit fini(void)
  1757. {
  1758. unregister_virtio_driver(&virtio_console);
  1759. class_destroy(pdrvdata.class);
  1760. if (pdrvdata.debugfs_dir)
  1761. debugfs_remove_recursive(pdrvdata.debugfs_dir);
  1762. }
  1763. module_init(init);
  1764. module_exit(fini);
  1765. MODULE_DEVICE_TABLE(virtio, id_table);
  1766. MODULE_DESCRIPTION("Virtio console driver");
  1767. MODULE_LICENSE("GPL");