eeepc-laptop: don't enable camera at startup if it's already on.
[safe/jmp/linux-2.6] / drivers / staging / hv / vmbus_drv.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/sysctl.h>
27 #include "osd.h"
28 #include "logging.h"
29 #include "vmbus.h"
30
31
32 /* FIXME! We need to do this dynamically for PIC and APIC system */
33 #define VMBUS_IRQ               0x5
34 #define VMBUS_IRQ_VECTOR        IRQ5_VECTOR
35
36 /* Main vmbus driver data structure */
37 struct vmbus_driver_context {
38         /* !! These must be the first 2 fields !! */
39         /* FIXME, this is a bug */
40         /* The driver field is not used in here. Instead, the bus field is */
41         /* used to represent the driver */
42         struct driver_context drv_ctx;
43         struct vmbus_driver drv_obj;
44
45         struct bus_type bus;
46         struct tasklet_struct msg_dpc;
47         struct tasklet_struct event_dpc;
48
49         /* The bus root device */
50         struct device_context device_ctx;
51 };
52
53 static int vmbus_match(struct device *device, struct device_driver *driver);
54 static int vmbus_probe(struct device *device);
55 static int vmbus_remove(struct device *device);
56 static void vmbus_shutdown(struct device *device);
57 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env);
58 static void vmbus_msg_dpc(unsigned long data);
59 static void vmbus_event_dpc(unsigned long data);
60
61 static irqreturn_t vmbus_isr(int irq, void *dev_id);
62
63 static void vmbus_device_release(struct device *device);
64 static void vmbus_bus_release(struct device *device);
65
66 static struct hv_device *vmbus_child_device_create(struct hv_guid *type,
67                                                    struct hv_guid *instance,
68                                                    void *context);
69 static void vmbus_child_device_destroy(struct hv_device *device_obj);
70 static int vmbus_child_device_register(struct hv_device *root_device_obj,
71                                        struct hv_device *child_device_obj);
72 static void vmbus_child_device_unregister(struct hv_device *child_device_obj);
73 static void vmbus_child_device_get_info(struct hv_device *device_obj,
74                                         struct hv_device_info *device_info);
75 static ssize_t vmbus_show_device_attr(struct device *dev,
76                                       struct device_attribute *dev_attr,
77                                       char *buf);
78
79
80 unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL);
81 EXPORT_SYMBOL(vmbus_loglevel);
82         /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */
83         /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */
84
85 static int vmbus_irq = VMBUS_IRQ;
86
87 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
88 static struct device_attribute vmbus_device_attrs[] = {
89         __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
90         __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
91         __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
92         __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
93         __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
94
95         __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
96         __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
97         __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
98
99         __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
100         __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
101         __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
102
103         __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
104         __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
105         __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
106         __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
107         __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
108
109         __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
110         __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
111         __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
112         __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
113         __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
114         __ATTR_NULL
115 };
116
117 /* The one and only one */
118 static struct vmbus_driver_context g_vmbus_drv = {
119         .bus.name =             "vmbus",
120         .bus.match =            vmbus_match,
121         .bus.shutdown =         vmbus_shutdown,
122         .bus.remove =           vmbus_remove,
123         .bus.probe =            vmbus_probe,
124         .bus.uevent =           vmbus_uevent,
125         .bus.dev_attrs =        vmbus_device_attrs,
126 };
127
128 /**
129  * vmbus_show_device_attr - Show the device attribute in sysfs.
130  *
131  * This is invoked when user does a
132  * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
133  */
134 static ssize_t vmbus_show_device_attr(struct device *dev,
135                                       struct device_attribute *dev_attr,
136                                       char *buf)
137 {
138         struct device_context *device_ctx = device_to_device_context(dev);
139         struct hv_device_info device_info;
140
141         memset(&device_info, 0, sizeof(struct hv_device_info));
142
143         vmbus_child_device_get_info(&device_ctx->device_obj, &device_info);
144
145         if (!strcmp(dev_attr->attr.name, "class_id")) {
146                 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
147                                "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
148                                device_info.ChannelType.data[3],
149                                device_info.ChannelType.data[2],
150                                device_info.ChannelType.data[1],
151                                device_info.ChannelType.data[0],
152                                device_info.ChannelType.data[5],
153                                device_info.ChannelType.data[4],
154                                device_info.ChannelType.data[7],
155                                device_info.ChannelType.data[6],
156                                device_info.ChannelType.data[8],
157                                device_info.ChannelType.data[9],
158                                device_info.ChannelType.data[10],
159                                device_info.ChannelType.data[11],
160                                device_info.ChannelType.data[12],
161                                device_info.ChannelType.data[13],
162                                device_info.ChannelType.data[14],
163                                device_info.ChannelType.data[15]);
164         } else if (!strcmp(dev_attr->attr.name, "device_id")) {
165                 return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
166                                "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
167                                device_info.ChannelInstance.data[3],
168                                device_info.ChannelInstance.data[2],
169                                device_info.ChannelInstance.data[1],
170                                device_info.ChannelInstance.data[0],
171                                device_info.ChannelInstance.data[5],
172                                device_info.ChannelInstance.data[4],
173                                device_info.ChannelInstance.data[7],
174                                device_info.ChannelInstance.data[6],
175                                device_info.ChannelInstance.data[8],
176                                device_info.ChannelInstance.data[9],
177                                device_info.ChannelInstance.data[10],
178                                device_info.ChannelInstance.data[11],
179                                device_info.ChannelInstance.data[12],
180                                device_info.ChannelInstance.data[13],
181                                device_info.ChannelInstance.data[14],
182                                device_info.ChannelInstance.data[15]);
183         } else if (!strcmp(dev_attr->attr.name, "state")) {
184                 return sprintf(buf, "%d\n", device_info.ChannelState);
185         } else if (!strcmp(dev_attr->attr.name, "id")) {
186                 return sprintf(buf, "%d\n", device_info.ChannelId);
187         } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
188                 return sprintf(buf, "%d\n", device_info.Outbound.InterruptMask);
189         } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
190                 return sprintf(buf, "%d\n", device_info.Outbound.ReadIndex);
191         } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
192                 return sprintf(buf, "%d\n", device_info.Outbound.WriteIndex);
193         } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
194                 return sprintf(buf, "%d\n",
195                                device_info.Outbound.BytesAvailToRead);
196         } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
197                 return sprintf(buf, "%d\n",
198                                device_info.Outbound.BytesAvailToWrite);
199         } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
200                 return sprintf(buf, "%d\n", device_info.Inbound.InterruptMask);
201         } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
202                 return sprintf(buf, "%d\n", device_info.Inbound.ReadIndex);
203         } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
204                 return sprintf(buf, "%d\n", device_info.Inbound.WriteIndex);
205         } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
206                 return sprintf(buf, "%d\n",
207                                device_info.Inbound.BytesAvailToRead);
208         } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
209                 return sprintf(buf, "%d\n",
210                                device_info.Inbound.BytesAvailToWrite);
211         } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
212                 return sprintf(buf, "%d\n", device_info.MonitorId);
213         } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
214                 return sprintf(buf, "%d\n", device_info.ServerMonitorPending);
215         } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
216                 return sprintf(buf, "%d\n", device_info.ServerMonitorLatency);
217         } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
218                 return sprintf(buf, "%d\n",
219                                device_info.ServerMonitorConnectionId);
220         } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
221                 return sprintf(buf, "%d\n", device_info.ClientMonitorPending);
222         } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
223                 return sprintf(buf, "%d\n", device_info.ClientMonitorLatency);
224         } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
225                 return sprintf(buf, "%d\n",
226                                device_info.ClientMonitorConnectionId);
227         } else {
228                 return 0;
229         }
230 }
231
232 /**
233  * vmbus_bus_init -Main vmbus driver initialization routine.
234  *
235  * Here, we
236  *      - initialize the vmbus driver context
237  *      - setup various driver entry points
238  *      - invoke the vmbus hv main init routine
239  *      - get the irq resource
240  *      - invoke the vmbus to add the vmbus root device
241  *      - setup the vmbus root device
242  *      - retrieve the channel offers
243  */
244 static int vmbus_bus_init(int (*drv_init)(struct hv_driver *drv))
245 {
246         struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
247         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
248         struct device_context *dev_ctx = &g_vmbus_drv.device_ctx;
249         int ret;
250         unsigned int vector;
251
252         DPRINT_ENTER(VMBUS_DRV);
253
254         /*
255          * Set this up to allow lower layer to callback to add/remove child
256          * devices on the bus
257          */
258         vmbus_drv_obj->OnChildDeviceCreate = vmbus_child_device_create;
259         vmbus_drv_obj->OnChildDeviceDestroy = vmbus_child_device_destroy;
260         vmbus_drv_obj->OnChildDeviceAdd = vmbus_child_device_register;
261         vmbus_drv_obj->OnChildDeviceRemove = vmbus_child_device_unregister;
262
263         /* Call to bus driver to initialize */
264         ret = drv_init(&vmbus_drv_obj->Base);
265         if (ret != 0) {
266                 DPRINT_ERR(VMBUS_DRV, "Unable to initialize vmbus (%d)", ret);
267                 goto cleanup;
268         }
269
270         /* Sanity checks */
271         if (!vmbus_drv_obj->Base.OnDeviceAdd) {
272                 DPRINT_ERR(VMBUS_DRV, "OnDeviceAdd() routine not set");
273                 ret = -1;
274                 goto cleanup;
275         }
276
277         vmbus_drv_ctx->bus.name = vmbus_drv_obj->Base.name;
278
279         /* Initialize the bus context */
280         tasklet_init(&vmbus_drv_ctx->msg_dpc, vmbus_msg_dpc,
281                      (unsigned long)vmbus_drv_obj);
282         tasklet_init(&vmbus_drv_ctx->event_dpc, vmbus_event_dpc,
283                      (unsigned long)vmbus_drv_obj);
284
285         /* Now, register the bus driver with LDM */
286         ret = bus_register(&vmbus_drv_ctx->bus);
287         if (ret) {
288                 ret = -1;
289                 goto cleanup;
290         }
291
292         /* Get the interrupt resource */
293         ret = request_irq(vmbus_irq, vmbus_isr, IRQF_SAMPLE_RANDOM,
294                           vmbus_drv_obj->Base.name, NULL);
295
296         if (ret != 0) {
297                 DPRINT_ERR(VMBUS_DRV, "ERROR - Unable to request IRQ %d",
298                            vmbus_irq);
299
300                 bus_unregister(&vmbus_drv_ctx->bus);
301
302                 ret = -1;
303                 goto cleanup;
304         }
305         vector = VMBUS_IRQ_VECTOR;
306
307         DPRINT_INFO(VMBUS_DRV, "irq 0x%x vector 0x%x", vmbus_irq, vector);
308
309         /* Call to bus driver to add the root device */
310         memset(dev_ctx, 0, sizeof(struct device_context));
311
312         ret = vmbus_drv_obj->Base.OnDeviceAdd(&dev_ctx->device_obj, &vector);
313         if (ret != 0) {
314                 DPRINT_ERR(VMBUS_DRV,
315                            "ERROR - Unable to add vmbus root device");
316
317                 free_irq(vmbus_irq, NULL);
318
319                 bus_unregister(&vmbus_drv_ctx->bus);
320
321                 ret = -1;
322                 goto cleanup;
323         }
324         /* strcpy(dev_ctx->device.bus_id, dev_ctx->device_obj.name); */
325         dev_set_name(&dev_ctx->device, "vmbus_0_0");
326         memcpy(&dev_ctx->class_id, &dev_ctx->device_obj.deviceType,
327                 sizeof(struct hv_guid));
328         memcpy(&dev_ctx->device_id, &dev_ctx->device_obj.deviceInstance,
329                 sizeof(struct hv_guid));
330
331         /* No need to bind a driver to the root device. */
332         dev_ctx->device.parent = NULL;
333         /* NULL; vmbus_remove() does not get invoked */
334         dev_ctx->device.bus = &vmbus_drv_ctx->bus;
335
336         /* Setup the device dispatch table */
337         dev_ctx->device.release = vmbus_bus_release;
338
339         /* Setup the bus as root device */
340         ret = device_register(&dev_ctx->device);
341         if (ret) {
342                 DPRINT_ERR(VMBUS_DRV,
343                            "ERROR - Unable to register vmbus root device");
344
345                 free_irq(vmbus_irq, NULL);
346                 bus_unregister(&vmbus_drv_ctx->bus);
347
348                 ret = -1;
349                 goto cleanup;
350         }
351
352
353         vmbus_drv_obj->GetChannelOffers();
354
355 cleanup:
356         DPRINT_EXIT(VMBUS_DRV);
357
358         return ret;
359 }
360
361 /**
362  * vmbus_bus_exit - Terminate the vmbus driver.
363  *
364  * This routine is opposite of vmbus_bus_init()
365  */
366 static void vmbus_bus_exit(void)
367 {
368         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
369         struct vmbus_driver_context *vmbus_drv_ctx = &g_vmbus_drv;
370
371         struct device_context *dev_ctx = &g_vmbus_drv.device_ctx;
372
373         DPRINT_ENTER(VMBUS_DRV);
374
375         /* Remove the root device */
376         if (vmbus_drv_obj->Base.OnDeviceRemove)
377                 vmbus_drv_obj->Base.OnDeviceRemove(&dev_ctx->device_obj);
378
379         if (vmbus_drv_obj->Base.OnCleanup)
380                 vmbus_drv_obj->Base.OnCleanup(&vmbus_drv_obj->Base);
381
382         /* Unregister the root bus device */
383         device_unregister(&dev_ctx->device);
384
385         bus_unregister(&vmbus_drv_ctx->bus);
386
387         free_irq(vmbus_irq, NULL);
388
389         tasklet_kill(&vmbus_drv_ctx->msg_dpc);
390         tasklet_kill(&vmbus_drv_ctx->event_dpc);
391
392         DPRINT_EXIT(VMBUS_DRV);
393
394         return;
395 }
396
397 /**
398  * vmbus_child_driver_register - Register a vmbus's child driver
399  */
400 int vmbus_child_driver_register(struct driver_context *driver_ctx)
401 {
402         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
403         int ret;
404
405         DPRINT_ENTER(VMBUS_DRV);
406
407         DPRINT_INFO(VMBUS_DRV, "child driver (%p) registering - name %s",
408                     driver_ctx, driver_ctx->driver.name);
409
410         /* The child driver on this vmbus */
411         driver_ctx->driver.bus = &g_vmbus_drv.bus;
412
413         ret = driver_register(&driver_ctx->driver);
414
415         vmbus_drv_obj->GetChannelOffers();
416
417         DPRINT_EXIT(VMBUS_DRV);
418
419         return ret;
420 }
421 EXPORT_SYMBOL(vmbus_child_driver_register);
422
423 /**
424  * vmbus_child_driver_unregister Unregister a vmbus's child driver
425  */
426 void vmbus_child_driver_unregister(struct driver_context *driver_ctx)
427 {
428         DPRINT_ENTER(VMBUS_DRV);
429
430         DPRINT_INFO(VMBUS_DRV, "child driver (%p) unregistering - name %s",
431                     driver_ctx, driver_ctx->driver.name);
432
433         driver_unregister(&driver_ctx->driver);
434
435         driver_ctx->driver.bus = NULL;
436
437         DPRINT_EXIT(VMBUS_DRV);
438 }
439 EXPORT_SYMBOL(vmbus_child_driver_unregister);
440
441 /**
442  * vmbus_get_interface - Get the vmbus channel interface.
443  *
444  * This is invoked by child/client driver that sits above vmbus
445  */
446 void vmbus_get_interface(struct vmbus_channel_interface *interface)
447 {
448         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
449
450         vmbus_drv_obj->GetChannelInterface(interface);
451 }
452 EXPORT_SYMBOL(vmbus_get_interface);
453
454 /**
455  * vmbus_child_device_get_info - Get the vmbus child device info.
456  *
457  * This is invoked to display various device attributes in sysfs.
458  */
459 static void vmbus_child_device_get_info(struct hv_device *device_obj,
460                                         struct hv_device_info *device_info)
461 {
462         struct vmbus_driver *vmbus_drv_obj = &g_vmbus_drv.drv_obj;
463
464         vmbus_drv_obj->GetChannelInfo(device_obj, device_info);
465 }
466
467 /**
468  * vmbus_child_device_create - Creates and registers a new child device on the vmbus.
469  */
470 static struct hv_device *vmbus_child_device_create(struct hv_guid *type,
471                                                    struct hv_guid *instance,
472                                                    void *context)
473 {
474         struct device_context *child_device_ctx;
475         struct hv_device *child_device_obj;
476
477         DPRINT_ENTER(VMBUS_DRV);
478
479         /* Allocate the new child device */
480         child_device_ctx = kzalloc(sizeof(struct device_context), GFP_KERNEL);
481         if (!child_device_ctx) {
482                 DPRINT_ERR(VMBUS_DRV,
483                         "unable to allocate device_context for child device");
484                 DPRINT_EXIT(VMBUS_DRV);
485
486                 return NULL;
487         }
488
489         DPRINT_DBG(VMBUS_DRV, "child device (%p) allocated - "
490                 "type {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
491                 "%02x%02x%02x%02x%02x%02x%02x%02x},"
492                 "id {%02x%02x%02x%02x-%02x%02x-%02x%02x-"
493                 "%02x%02x%02x%02x%02x%02x%02x%02x}",
494                 &child_device_ctx->device,
495                 type->data[3], type->data[2], type->data[1], type->data[0],
496                 type->data[5], type->data[4], type->data[7], type->data[6],
497                 type->data[8], type->data[9], type->data[10], type->data[11],
498                 type->data[12], type->data[13], type->data[14], type->data[15],
499                 instance->data[3], instance->data[2],
500                 instance->data[1], instance->data[0],
501                 instance->data[5], instance->data[4],
502                 instance->data[7], instance->data[6],
503                 instance->data[8], instance->data[9],
504                 instance->data[10], instance->data[11],
505                 instance->data[12], instance->data[13],
506                 instance->data[14], instance->data[15]);
507
508         child_device_obj = &child_device_ctx->device_obj;
509         child_device_obj->context = context;
510         memcpy(&child_device_obj->deviceType, &type, sizeof(struct hv_guid));
511         memcpy(&child_device_obj->deviceInstance, &instance,
512                sizeof(struct hv_guid));
513
514         memcpy(&child_device_ctx->class_id, &type, sizeof(struct hv_guid));
515         memcpy(&child_device_ctx->device_id, &instance, sizeof(struct hv_guid));
516
517         DPRINT_EXIT(VMBUS_DRV);
518
519         return child_device_obj;
520 }
521
522 /**
523  * vmbus_child_device_register - Register the child device on the specified bus
524  */
525 static int vmbus_child_device_register(struct hv_device *root_device_obj,
526                                        struct hv_device *child_device_obj)
527 {
528         int ret = 0;
529         struct device_context *root_device_ctx =
530                                 to_device_context(root_device_obj);
531         struct device_context *child_device_ctx =
532                                 to_device_context(child_device_obj);
533         static atomic_t device_num = ATOMIC_INIT(0);
534
535         DPRINT_ENTER(VMBUS_DRV);
536
537         DPRINT_DBG(VMBUS_DRV, "child device (%p) registering",
538                    child_device_ctx);
539
540         /* Make sure we are not registered already */
541         if (strlen(dev_name(&child_device_ctx->device)) != 0) {
542                 DPRINT_ERR(VMBUS_DRV,
543                            "child device (%p) already registered - busid %s",
544                            child_device_ctx,
545                            dev_name(&child_device_ctx->device));
546
547                 ret = -1;
548                 goto Cleanup;
549         }
550
551         /* Set the device bus id. Otherwise, device_register()will fail. */
552         dev_set_name(&child_device_ctx->device, "vmbus_0_%d",
553                      atomic_inc_return(&device_num));
554
555         /* The new device belongs to this bus */
556         child_device_ctx->device.bus = &g_vmbus_drv.bus; /* device->dev.bus; */
557         child_device_ctx->device.parent = &root_device_ctx->device;
558         child_device_ctx->device.release = vmbus_device_release;
559
560         /*
561          * Register with the LDM. This will kick off the driver/device
562          * binding...which will eventually call vmbus_match() and vmbus_probe()
563          */
564         ret = device_register(&child_device_ctx->device);
565
566         /* vmbus_probe() error does not get propergate to device_register(). */
567         ret = child_device_ctx->probe_error;
568
569         if (ret)
570                 DPRINT_ERR(VMBUS_DRV, "unable to register child device (%p)",
571                            &child_device_ctx->device);
572         else
573                 DPRINT_INFO(VMBUS_DRV, "child device (%p) registered",
574                             &child_device_ctx->device);
575
576 Cleanup:
577         DPRINT_EXIT(VMBUS_DRV);
578
579         return ret;
580 }
581
582 /**
583  * vmbus_child_device_unregister - Remove the specified child device from the vmbus.
584  */
585 static void vmbus_child_device_unregister(struct hv_device *device_obj)
586 {
587         struct device_context *device_ctx = to_device_context(device_obj);
588
589         DPRINT_ENTER(VMBUS_DRV);
590
591         DPRINT_INFO(VMBUS_DRV, "unregistering child device (%p)",
592                     &device_ctx->device);
593
594         /*
595          * Kick off the process of unregistering the device.
596          * This will call vmbus_remove() and eventually vmbus_device_release()
597          */
598         device_unregister(&device_ctx->device);
599
600         DPRINT_INFO(VMBUS_DRV, "child device (%p) unregistered",
601                     &device_ctx->device);
602
603         DPRINT_EXIT(VMBUS_DRV);
604 }
605
606 /**
607  * vmbus_child_device_destroy - Destroy the specified child device on the vmbus.
608  */
609 static void vmbus_child_device_destroy(struct hv_device *device_obj)
610 {
611         DPRINT_ENTER(VMBUS_DRV);
612
613         DPRINT_EXIT(VMBUS_DRV);
614 }
615
616 /**
617  * vmbus_uevent - add uevent for our device
618  *
619  * This routine is invoked when a device is added or removed on the vmbus to
620  * generate a uevent to udev in the userspace. The udev will then look at its
621  * rule and the uevent generated here to load the appropriate driver
622  */
623 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
624 {
625         struct device_context *device_ctx = device_to_device_context(device);
626         int i = 0;
627         int len = 0;
628         int ret;
629
630         DPRINT_ENTER(VMBUS_DRV);
631
632         DPRINT_INFO(VMBUS_DRV, "generating uevent - VMBUS_DEVICE_CLASS_GUID={"
633                     "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
634                     "%02x%02x%02x%02x%02x%02x%02x%02x}",
635                     device_ctx->class_id.data[3], device_ctx->class_id.data[2],
636                     device_ctx->class_id.data[1], device_ctx->class_id.data[0],
637                     device_ctx->class_id.data[5], device_ctx->class_id.data[4],
638                     device_ctx->class_id.data[7], device_ctx->class_id.data[6],
639                     device_ctx->class_id.data[8], device_ctx->class_id.data[9],
640                     device_ctx->class_id.data[10],
641                     device_ctx->class_id.data[11],
642                     device_ctx->class_id.data[12],
643                     device_ctx->class_id.data[13],
644                     device_ctx->class_id.data[14],
645                     device_ctx->class_id.data[15]);
646
647         env->envp_idx = i;
648         env->buflen = len;
649         ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={"
650                              "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
651                              "%02x%02x%02x%02x%02x%02x%02x%02x}",
652                              device_ctx->class_id.data[3],
653                              device_ctx->class_id.data[2],
654                              device_ctx->class_id.data[1],
655                              device_ctx->class_id.data[0],
656                              device_ctx->class_id.data[5],
657                              device_ctx->class_id.data[4],
658                              device_ctx->class_id.data[7],
659                              device_ctx->class_id.data[6],
660                              device_ctx->class_id.data[8],
661                              device_ctx->class_id.data[9],
662                              device_ctx->class_id.data[10],
663                              device_ctx->class_id.data[11],
664                              device_ctx->class_id.data[12],
665                              device_ctx->class_id.data[13],
666                              device_ctx->class_id.data[14],
667                              device_ctx->class_id.data[15]);
668
669         if (ret)
670                 return ret;
671
672         ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={"
673                              "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
674                              "%02x%02x%02x%02x%02x%02x%02x%02x}",
675                              device_ctx->device_id.data[3],
676                              device_ctx->device_id.data[2],
677                              device_ctx->device_id.data[1],
678                              device_ctx->device_id.data[0],
679                              device_ctx->device_id.data[5],
680                              device_ctx->device_id.data[4],
681                              device_ctx->device_id.data[7],
682                              device_ctx->device_id.data[6],
683                              device_ctx->device_id.data[8],
684                              device_ctx->device_id.data[9],
685                              device_ctx->device_id.data[10],
686                              device_ctx->device_id.data[11],
687                              device_ctx->device_id.data[12],
688                              device_ctx->device_id.data[13],
689                              device_ctx->device_id.data[14],
690                              device_ctx->device_id.data[15]);
691         if (ret)
692                 return ret;
693
694         env->envp[env->envp_idx] = NULL;
695
696         DPRINT_EXIT(VMBUS_DRV);
697
698         return 0;
699 }
700
701 /**
702  * vmbus_match - Attempt to match the specified device to the specified driver
703  */
704 static int vmbus_match(struct device *device, struct device_driver *driver)
705 {
706         int match = 0;
707         struct driver_context *driver_ctx = driver_to_driver_context(driver);
708         struct device_context *device_ctx = device_to_device_context(device);
709
710         DPRINT_ENTER(VMBUS_DRV);
711
712         /* We found our driver ? */
713         if (memcmp(&device_ctx->class_id, &driver_ctx->class_id,
714                    sizeof(struct hv_guid)) == 0) {
715                 /*
716                  * !! NOTE: The driver_ctx is not a vmbus_drv_ctx. We typecast
717                  * it here to access the struct hv_driver field
718                  */
719                 struct vmbus_driver_context *vmbus_drv_ctx =
720                         (struct vmbus_driver_context *)driver_ctx;
721
722                 device_ctx->device_obj.Driver = &vmbus_drv_ctx->drv_obj.Base;
723                 DPRINT_INFO(VMBUS_DRV,
724                             "device object (%p) set to driver object (%p)",
725                             &device_ctx->device_obj,
726                             device_ctx->device_obj.Driver);
727
728                 match = 1;
729         }
730
731         DPRINT_EXIT(VMBUS_DRV);
732
733         return match;
734 }
735
736 /**
737  * vmbus_probe_failed_cb - Callback when a driver probe failed in vmbus_probe()
738  *
739  * We need a callback because we cannot invoked device_unregister() inside
740  * vmbus_probe() since vmbus_probe() may be invoked inside device_register()
741  * i.e. we cannot call device_unregister() inside device_register()
742  */
743 static void vmbus_probe_failed_cb(struct work_struct *context)
744 {
745         struct device_context *device_ctx = (struct device_context *)context;
746
747         DPRINT_ENTER(VMBUS_DRV);
748
749         /*
750          * Kick off the process of unregistering the device.
751          * This will call vmbus_remove() and eventually vmbus_device_release()
752          */
753         device_unregister(&device_ctx->device);
754
755         /* put_device(&device_ctx->device); */
756         DPRINT_EXIT(VMBUS_DRV);
757 }
758
759 /**
760  * vmbus_probe - Add the new vmbus's child device
761  */
762 static int vmbus_probe(struct device *child_device)
763 {
764         int ret = 0;
765         struct driver_context *driver_ctx =
766                         driver_to_driver_context(child_device->driver);
767         struct device_context *device_ctx =
768                         device_to_device_context(child_device);
769
770         DPRINT_ENTER(VMBUS_DRV);
771
772         /* Let the specific open-source driver handles the probe if it can */
773         if (driver_ctx->probe) {
774                 ret = device_ctx->probe_error = driver_ctx->probe(child_device);
775                 if (ret != 0) {
776                         DPRINT_ERR(VMBUS_DRV, "probe() failed for device %s "
777                                    "(%p) on driver %s (%d)...",
778                                    dev_name(child_device), child_device,
779                                    child_device->driver->name, ret);
780
781                         INIT_WORK(&device_ctx->probe_failed_work_item,
782                                   vmbus_probe_failed_cb);
783                         schedule_work(&device_ctx->probe_failed_work_item);
784                 }
785         } else {
786                 DPRINT_ERR(VMBUS_DRV, "probe() method not set for driver - %s",
787                            child_device->driver->name);
788                 ret = -1;
789         }
790
791         DPRINT_EXIT(VMBUS_DRV);
792         return ret;
793 }
794
795 /**
796  * vmbus_remove - Remove a vmbus device
797  */
798 static int vmbus_remove(struct device *child_device)
799 {
800         int ret;
801         struct driver_context *driver_ctx;
802
803         DPRINT_ENTER(VMBUS_DRV);
804
805         /* Special case root bus device */
806         if (child_device->parent == NULL) {
807                 /*
808                  * No-op since it is statically defined and handle in
809                  * vmbus_bus_exit()
810                  */
811                 DPRINT_EXIT(VMBUS_DRV);
812                 return 0;
813         }
814
815         if (child_device->driver) {
816                 driver_ctx = driver_to_driver_context(child_device->driver);
817
818                 /*
819                  * Let the specific open-source driver handles the removal if
820                  * it can
821                  */
822                 if (driver_ctx->remove) {
823                         ret = driver_ctx->remove(child_device);
824                 } else {
825                         DPRINT_ERR(VMBUS_DRV,
826                                    "remove() method not set for driver - %s",
827                                    child_device->driver->name);
828                         ret = -1;
829                 }
830         }
831
832         DPRINT_EXIT(VMBUS_DRV);
833
834         return 0;
835 }
836
837 /**
838  * vmbus_shutdown - Shutdown a vmbus device
839  */
840 static void vmbus_shutdown(struct device *child_device)
841 {
842         struct driver_context *driver_ctx;
843
844         DPRINT_ENTER(VMBUS_DRV);
845
846         /* Special case root bus device */
847         if (child_device->parent == NULL) {
848                 /*
849                  * No-op since it is statically defined and handle in
850                  * vmbus_bus_exit()
851                  */
852                 DPRINT_EXIT(VMBUS_DRV);
853                 return;
854         }
855
856         /* The device may not be attached yet */
857         if (!child_device->driver) {
858                 DPRINT_EXIT(VMBUS_DRV);
859                 return;
860         }
861
862         driver_ctx = driver_to_driver_context(child_device->driver);
863
864         /* Let the specific open-source driver handles the removal if it can */
865         if (driver_ctx->shutdown)
866                 driver_ctx->shutdown(child_device);
867
868         DPRINT_EXIT(VMBUS_DRV);
869
870         return;
871 }
872
873 /**
874  * vmbus_bus_release - Final callback release of the vmbus root device
875  */
876 static void vmbus_bus_release(struct device *device)
877 {
878         DPRINT_ENTER(VMBUS_DRV);
879         /* FIXME */
880         /* Empty release functions are a bug, or a major sign
881          * of a problem design, this MUST BE FIXED! */
882         dev_err(device, "%s needs to be fixed!\n", __func__);
883         WARN_ON(1);
884         DPRINT_EXIT(VMBUS_DRV);
885 }
886
887 /**
888  * vmbus_device_release - Final callback release of the vmbus child device
889  */
890 static void vmbus_device_release(struct device *device)
891 {
892         struct device_context *device_ctx = device_to_device_context(device);
893
894         DPRINT_ENTER(VMBUS_DRV);
895
896         /* vmbus_child_device_destroy(&device_ctx->device_obj); */
897         kfree(device_ctx);
898
899         /* !!DO NOT REFERENCE device_ctx anymore at this point!! */
900         DPRINT_EXIT(VMBUS_DRV);
901
902         return;
903 }
904
905 /**
906  * vmbus_msg_dpc - Tasklet routine to handle hypervisor messages
907  */
908 static void vmbus_msg_dpc(unsigned long data)
909 {
910         struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
911
912         DPRINT_ENTER(VMBUS_DRV);
913
914         ASSERT(vmbus_drv_obj->OnMsgDpc != NULL);
915
916         /* Call to bus driver to handle interrupt */
917         vmbus_drv_obj->OnMsgDpc(&vmbus_drv_obj->Base);
918
919         DPRINT_EXIT(VMBUS_DRV);
920 }
921
922 /**
923  * vmbus_msg_dpc - Tasklet routine to handle hypervisor events
924  */
925 static void vmbus_event_dpc(unsigned long data)
926 {
927         struct vmbus_driver *vmbus_drv_obj = (struct vmbus_driver *)data;
928
929         DPRINT_ENTER(VMBUS_DRV);
930
931         ASSERT(vmbus_drv_obj->OnEventDpc != NULL);
932
933         /* Call to bus driver to handle interrupt */
934         vmbus_drv_obj->OnEventDpc(&vmbus_drv_obj->Base);
935
936         DPRINT_EXIT(VMBUS_DRV);
937 }
938
939 static irqreturn_t vmbus_isr(int irq, void *dev_id)
940 {
941         struct vmbus_driver *vmbus_driver_obj = &g_vmbus_drv.drv_obj;
942         int ret;
943
944         DPRINT_ENTER(VMBUS_DRV);
945
946         ASSERT(vmbus_driver_obj->OnIsr != NULL);
947
948         /* Call to bus driver to handle interrupt */
949         ret = vmbus_driver_obj->OnIsr(&vmbus_driver_obj->Base);
950
951         /* Schedules a dpc if necessary */
952         if (ret > 0) {
953                 if (test_bit(0, (unsigned long *)&ret))
954                         tasklet_schedule(&g_vmbus_drv.msg_dpc);
955
956                 if (test_bit(1, (unsigned long *)&ret))
957                         tasklet_schedule(&g_vmbus_drv.event_dpc);
958
959                 DPRINT_EXIT(VMBUS_DRV);
960                 return IRQ_HANDLED;
961         } else {
962                 DPRINT_EXIT(VMBUS_DRV);
963                 return IRQ_NONE;
964         }
965 }
966
967 static int __init vmbus_init(void)
968 {
969         int ret = 0;
970
971         DPRINT_ENTER(VMBUS_DRV);
972
973         DPRINT_INFO(VMBUS_DRV,
974                 "Vmbus initializing.... current log level 0x%x (%x,%x)",
975                 vmbus_loglevel, HIWORD(vmbus_loglevel), LOWORD(vmbus_loglevel));
976         /* Todo: it is used for loglevel, to be ported to new kernel. */
977
978         ret = vmbus_bus_init(VmbusInitialize);
979
980         DPRINT_EXIT(VMBUS_DRV);
981         return ret;
982 }
983
984 static void __exit vmbus_exit(void)
985 {
986         DPRINT_ENTER(VMBUS_DRV);
987
988         vmbus_bus_exit();
989         /* Todo: it is used for loglevel, to be ported to new kernel. */
990         DPRINT_EXIT(VMBUS_DRV);
991         return;
992 }
993
994 MODULE_LICENSE("GPL");
995 module_param(vmbus_irq, int, S_IRUGO);
996 module_param(vmbus_loglevel, int, S_IRUGO);
997
998 module_init(vmbus_init);
999 module_exit(vmbus_exit);