aboutsummaryrefslogtreecommitdiffstats
path: root/net/bridge/br_if.c
blob: ad1c7af65ec8dcb944b59d2310df3c9ec539805d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 *	Userspace interface
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *
 *	$Id: br_if.c,v 1.7 2001/12/24 00:59:55 davem Exp $
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/if_arp.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/if_ether.h>
#include <net/sock.h>

#include "br_private.h"

/*
 * Determine initial path cost based on speed.
 * using recommendations from 802.1d standard
 *
 * Need to simulate user ioctl because not all device's that support
 * ethtool, use ethtool_ops.  Also, since driver might sleep need to
 * not be holding any locks.
 */
static int port_cost(struct net_device *dev)
{
	struct ethtool_cmd ecmd = { ETHTOOL_GSET };
	struct ifreq ifr;
	mm_segment_t old_fs;
	int err;

	strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
	ifr.ifr_data = (void __user *) &ecmd;

	old_fs = get_fs();
	set_fs(KERNEL_DS);
	err = dev_ethtool(&ifr);
	set_fs(old_fs);
	
	if (!err) {
		switch(ecmd.speed) {
		case SPEED_100:
			return 19;
		case SPEED_1000:
			return 4;
		case SPEED_10000:
			return 2;
		case SPEED_10:
			return 100;
		}
	}

	/* Old silly heuristics based on name */
	if (!strncmp(dev->name, "lec", 3))
		return 7;

	if (!strncmp(dev->name, "plip", 4))
		return 2500;

	return 100;	/* assume old 10Mbps */
}


/*
 * Check for port carrier transistions.
 * Called from work queue to allow for calling functions that
 * might sleep (such as speed check), and to debounce.
 */
static void port_carrier_check(void *arg)
{
	struct net_device *dev = arg;
	struct net_bridge_port *p;
	struct net_bridge *br;

	rtnl_lock();
	p = dev->br_port;
	if (!p)
		goto done;
	br = p->br;

	if (netif_carrier_ok(dev))
		p->path_cost = port_cost(dev);

	if (br->dev->flags & IFF_UP) {
		spin_lock_bh(&br->lock);
		if (netif_carrier_ok(dev)) {
			if (p->state == BR_STATE_DISABLED)
				br_stp_enable_port(p);
		} else {
			if (p->state != BR_STATE_DISABLED)
				br_stp_disable_port(p);
		}
		spin_unlock_bh(&br->lock);
	}
done:
	rtnl_unlock();
}

static void release_nbp(struct kobject *kobj)
{
	struct net_bridge_port *p
		= container_of(kobj, struct net_bridge_port, kobj);
	kfree(p);
}

static struct kobj_type brport_ktype = {
#ifdef CONFIG_SYSFS
	.sysfs_ops = &brport_sysfs_ops,
#endif
	.release = release_nbp,
};

static void destroy_nbp(struct net_bridge_port *p)
{
	struct net_device *dev = p->dev;

	p->br = NULL;
	p->dev = NULL;
	dev_put(dev);

	kobject_put(&p->kobj);
}

static void destroy_nbp_rcu(struct rcu_head *head)
{
	struct net_bridge_port *p =
			container_of(head, struct net_bridge_port, rcu);
	destroy_nbp(p);
}

/* Delete port(interface) from bridge is done in two steps.
 * via RCU. First step, marks device as down. That deletes
 * all the timers and stops new packets from flowing through.
 *
 * Final cleanup doesn't occur until after all CPU's finished
 * processing packets.
 *
 * Protected from multiple admin operations by RTNL mutex
 */
static void del_nbp(struct net_bridge_port *p)
{
	struct net_bridge *br = p->br;
	struct net_device *dev = p->dev;

	sysfs_remove_link(&br->ifobj, dev->name);

	dev_set_promiscuity(dev, -1);

	cancel_delayed_work(&p->carrier_check);

	spin_lock_bh(&br->lock);
	br_stp_disable_port(p);
	spin_unlock_bh(&br->lock);

	br_fdb_delete_by_port(br, p);

	list_del_rcu(&p->list);

	rcu_assign_pointer(dev->br_port, NULL);

	kobject_uevent(&p->kobj, KOBJ_REMOVE);
	kobject_del(&p->kobj);

	call_rcu(&p->rcu, destroy_nbp_rcu);
}

/* called with RTNL */
static void del_br(struct net_bridge *br)
{
	struct net_bridge_port *p, *n;

	list_for_each_entry_safe(p, n, &br->port_list, list) {
		del_nbp(p);
	}

	del_timer_sync(&br->gc_timer);

	br_sysfs_delbr(br->dev);
 	unregister_netdevice(br->dev);
}

static struct net_device *new_bridge_dev(const char *name)
{
	struct net_bridge *br;
	struct net_device *dev;

	dev = alloc_netdev(sizeof(struct net_bridge), name,
			   br_dev_setup);
	
	if (!dev)
		return NULL;

	br = netdev_priv(dev);
	br->dev = dev;

	spin_lock_init(&br->lock);
	INIT_LIST_HEAD(&br->port_list);
	spin_lock_init(&br->hash_lock);

	br->bridge_id.prio[0] = 0x80;
	br->bridge_id.prio[1] = 0x00;

	memcpy(br->group_addr, br_group_address, ETH_ALEN);

	br->feature_mask = dev->features;
	br->stp_enabled = 0;
	br->designated_root = br->bridge_id;
	br->root_path_cost = 0;
	br->root_port = 0;
	br->bridge_max_age = br->max_age = 20 * HZ;
	br->bridge_hello_time = br->hello_time = 2 * HZ;
	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
	br->topology_change = 0;
	br->topology_change_detected = 0;
	br->ageing_time = 300 * HZ;
	INIT_LIST_HEAD(&br->age_list);

	br_stp_timer_init(br);

	return dev;
}

/* find an available port number */
static int find_portno(struct net_bridge *br)
{
	int index;
	struct net_bridge_port *p;
	unsigned long *inuse;

	inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
			GFP_KERNEL);
	if (!inuse)
		return -ENOMEM;

	set_bit(0, inuse);	/* zero is reserved */
	list_for_each_entry(p, &br->port_list, list) {
		set_bit(p->port_no, inuse);
	}
	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
	kfree(inuse);

	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
}

/* called with RTNL but without bridge lock */
static struct net_bridge_port *new_nbp(struct net_bridge *br, 
				       struct net_device *dev)
{
	int index;
	struct net_bridge_port *p;
	
	index = find_portno(br);
	if (index < 0)
		return ERR_PTR(index);

	p = kzalloc(sizeof(*p), GFP_KERNEL);
	if (p == NULL)
		return ERR_PTR(-ENOMEM);

	p->br = br;
	dev_hold(dev);
	p->dev = dev;
	p->path_cost = port_cost(dev);
 	p->priority = 0x8000 >> BR_PORT_BITS;
	p->port_no = index;
	br_init_port(p);
	p->state = BR_STATE_DISABLED;
	INIT_WORK(&p->carrier_check, port_carrier_check, dev);
	br_stp_port_timer_init(p);

	kobject_init(&p->kobj);
	kobject_set_name(&p->kobj, SYSFS_BRIDGE_PORT_ATTR);
	p->kobj.ktype = &brport_ktype;
	p->kobj.parent = &(dev->class_dev.kobj);
	p->kobj.kset = NULL;

	return p;
}

int br_add_bridge(const char *name)
{
	struct net_device *dev;
	int ret;

	dev = new_bridge_dev(name);
	if (!dev) 
		return -ENOMEM;

	rtnl_lock();
	if (strchr(dev->name, '%')) {
		ret = dev_alloc_name(dev, dev->name);
		if (ret < 0)
			goto err1;
	}

	ret = register_netdevice(dev);
	if (ret)
		goto err2;

	ret = br_sysfs_addbr(dev);
	if (ret)
		goto err3;
	rtnl_unlock();
	return 0;

 err3:
	unregister_netdev(dev);
 err2:
	free_netdev(dev);
 err1:
	rtnl_unlock();
	return ret;
}

int br_del_bridge(const char *name)
{
	struct net_device *dev;
	int ret = 0;

	rtnl_lock();
	dev = __dev_get_by_name(name);
	if (dev == NULL) 
		ret =  -ENXIO; 	/* Could not find device */

	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
		/* Attempt to delete non bridge device! */
		ret = -EPERM;
	}

	else if (dev->flags & IFF_UP) {
		/* Not shutdown yet. */
		ret = -EBUSY;
	} 

	else 
		del_br(netdev_priv(dev));

	rtnl_unlock();
	return ret;
}

/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
int br_min_mtu(const struct net_bridge *br)
{
	const struct net_bridge_port *p;
	int mtu = 0;

	ASSERT_RTNL();

	if (list_empty(&br->port_list))
		mtu = ETH_DATA_LEN;
	else {
		list_for_each_entry(p, &br->port_list, list) {
			if (!mtu  || p->dev->mtu < mtu)
				mtu = p->dev->mtu;
		}
	}
	return mtu;
}

/*
 * Recomputes features using slave's features
 */
void br_features_recompute(struct net_bridge *br)
{
	struct net_bridge_port *p;
	unsigned long features, checksum;

	features = br->feature_mask &~ NETIF_F_IP_CSUM;
	checksum = br->feature_mask & NETIF_F_IP_CSUM;

	list_for_each_entry(p, &br->port_list, list) {
		if (!(p->dev->features 
		      & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
			checksum = 0;
		features &= p->dev->features;
	}

	br->dev->features = features | checksum | NETIF_F_LLTX;
}

/* called with RTNL */
int br_add_if(struct net_bridge *br, struct net_device *dev)
{
	struct net_bridge_port *p;
	int err = 0;

	if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
		return -EINVAL;

	if (dev->hard_start_xmit == br_dev_xmit)
		return -ELOOP;

	if (dev->br_port != NULL)
		return -EBUSY;

	p = new_nbp(br, dev);
	if (IS_ERR(p))
		return PTR_ERR(p);

	err = kobject_add(&p->kobj);
	if (err)
		goto err0;

 	err = br_fdb_insert(br, p, dev->dev_addr);
	if (err)
		goto err1;

	err = br_sysfs_addif(p);
	if (err)
		goto err2;

	rcu_assign_pointer(dev->br_port, p);
	dev_set_promiscuity(dev, 1);

	list_add_rcu(&p->list, &br->port_list);

	spin_lock_bh(&br->lock);
	br_stp_recalculate_bridge_id(br);
	br_features_recompute(br);
	schedule_delayed_work(&p->carrier_check, BR_PORT_DEBOUNCE);
	spin_unlock_bh(&br->lock);

	dev_set_mtu(br->dev, br_min_mtu(br));
	kobject_uevent(&p->kobj, KOBJ_ADD);

	return 0;
err2:
	br_fdb_delete_by_port(br, p);
err1:
	kobject_del(&p->kobj);
err0:
	kobject_put(&p->kobj);
	return err;
}

/* called with RTNL */
int br_del_if(struct net_bridge *br, struct net_device *dev)
{
	struct net_bridge_port *p = dev->br_port;
	
	if (!p || p->br != br) 
		return -EINVAL;

	del_nbp(p);

	spin_lock_bh(&br->lock);
	br_stp_recalculate_bridge_id(br);
	br_features_recompute(br);
	spin_unlock_bh(&br->lock);

	return 0;
}

void __exit br_cleanup_bridges(void)
{
	struct net_device *dev, *nxt;

	rtnl_lock();
	for (dev = dev_base; dev; dev = nxt) {
		nxt = dev->next;
		if (dev->priv_flags & IFF_EBRIDGE)
			del_br(dev->priv);
	}
	rtnl_unlock();

}
>; } static int dmi_matched(const struct dmi_system_id *dmi) { quirks = dmi->driver_data; return 0; } static struct quirk_entry quirk_unknown = { }; static struct quirk_entry quirk_acer_aspire_1520 = { .brightness = -1, }; static struct quirk_entry quirk_acer_travelmate_2490 = { .mailled = 1, }; /* This AMW0 laptop has no bluetooth */ static struct quirk_entry quirk_medion_md_98300 = { .wireless = 1, }; static struct quirk_entry quirk_fujitsu_amilo_li_1718 = { .wireless = 2, }; static struct dmi_system_id acer_quirks[] = { { .callback = dmi_matched, .ident = "Acer Aspire 1360", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"), }, .driver_data = &quirk_acer_aspire_1520, }, { .callback = dmi_matched, .ident = "Acer Aspire 1520", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"), }, .driver_data = &quirk_acer_aspire_1520, }, { .callback = dmi_matched, .ident = "Acer Aspire 3100", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 3610", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3610"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 5100", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 5610", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5610"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 5630", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 5650", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 5680", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer Aspire 9110", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 9110"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer TravelMate 2490", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Acer TravelMate 4200", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Acer"), DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4200"), }, .driver_data = &quirk_acer_travelmate_2490, }, { .callback = dmi_matched, .ident = "Fujitsu Siemens Amilo Li 1718", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"), }, .driver_data = &quirk_fujitsu_amilo_li_1718, }, { .callback = dmi_matched, .ident = "Medion MD 98300", .matches = { DMI_MATCH(DMI_SYS_VENDOR, "MEDION"), DMI_MATCH(DMI_PRODUCT_NAME, "WAM2030"), }, .driver_data = &quirk_medion_md_98300, }, {} }; /* Find which quirks are needed for a particular vendor/ model pair */ static void find_quirks(void) { if (!force_series) { dmi_check_system(acer_quirks); } else if (force_series == 2490) { quirks = &quirk_acer_travelmate_2490; } if (quirks == NULL) quirks = &quirk_unknown; set_quirks(); } /* * General interface convenience methods */ static bool has_cap(u32 cap) { if ((interface->capability & cap) != 0) return 1; return 0; } /* * AMW0 (V1) interface */ struct wmab_args { u32 eax; u32 ebx; u32 ecx; u32 edx; }; struct wmab_ret { u32 eax; u32 ebx; u32 ecx; u32 edx; u32 eex; }; static acpi_status wmab_execute(struct wmab_args *regbuf, struct acpi_buffer *result) { struct acpi_buffer input; acpi_status status; input.length = sizeof(struct wmab_args); input.pointer = (u8 *)regbuf; status = wmi_evaluate_method(AMW0_GUID1, 1, 1, &input, result); return status; } static acpi_status AMW0_get_u32(u32 *value, u32 cap, struct wmi_interface *iface) { int err; u8 result; switch (cap) { case ACER_CAP_MAILLED: switch (quirks->mailled) { default: err = ec_read(0xA, &result); if (err) return AE_ERROR; *value = (result >> 7) & 0x1; return AE_OK; } break; case ACER_CAP_WIRELESS: switch (quirks->wireless) { case 1: err = ec_read(0x7B, &result); if (err) return AE_ERROR; *value = result & 0x1; return AE_OK; case 2: err = ec_read(0x71, &result); if (err) return AE_ERROR; *value = result & 0x1; return AE_OK; default: err = ec_read(0xA, &result); if (err) return AE_ERROR; *value = (result >> 2) & 0x1; return AE_OK; } break; case ACER_CAP_BLUETOOTH: switch (quirks->bluetooth) { default: err = ec_read(0xA, &result); if (err) return AE_ERROR; *value = (result >> 4) & 0x1; return AE_OK; } break; case ACER_CAP_BRIGHTNESS: switch (quirks->brightness) { default: err = ec_read(0x83, &result); if (err) return AE_ERROR; *value = result; return AE_OK; } break; default: return AE_ERROR; } return AE_OK; } static acpi_status AMW0_set_u32(u32 value, u32 cap, struct wmi_interface *iface) { struct wmab_args args; args.eax = ACER_AMW0_WRITE; args.ebx = value ? (1<<8) : 0; args.ecx = args.edx = 0; switch (cap) { case ACER_CAP_MAILLED: if (value > 1) return AE_BAD_PARAMETER; args.ebx |= ACER_AMW0_MAILLED_MASK; break; case ACER_CAP_WIRELESS: if (value > 1) return AE_BAD_PARAMETER; args.ebx |= ACER_AMW0_WIRELESS_MASK; break; case ACER_CAP_BLUETOOTH: if (value > 1) return AE_BAD_PARAMETER; args.ebx |= ACER_AMW0_BLUETOOTH_MASK; break; case ACER_CAP_BRIGHTNESS: if (value > max_brightness) return AE_BAD_PARAMETER; switch (quirks->brightness) { default: return ec_write(0x83, value); break; } default: return AE_ERROR; } /* Actually do the set */ return wmab_execute(&args, NULL); } static acpi_status AMW0_find_mailled(void) { struct wmab_args args; struct wmab_ret ret; acpi_status status = AE_OK; struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *obj; args.eax = 0x86; args.ebx = args.ecx = args.edx = 0; status = wmab_execute(&args, &out); if (ACPI_FAILURE(status)) return status; obj = (union acpi_object *) out.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(struct wmab_ret)) { ret = *((struct wmab_ret *) obj->buffer.pointer); } else { return AE_ERROR; } if (ret.eex & 0x1) interface->capability |= ACER_CAP_MAILLED; kfree(out.pointer); return AE_OK; } static acpi_status AMW0_set_capabilities(void) { struct wmab_args args; struct wmab_ret ret; acpi_status status = AE_OK; struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *obj; /* * On laptops with this strange GUID (non Acer), normal probing doesn't * work. */ if (wmi_has_guid(AMW0_GUID2)) { interface->capability |= ACER_CAP_WIRELESS; return AE_OK; } args.eax = ACER_AMW0_WRITE; args.ecx = args.edx = 0; args.ebx = 0xa2 << 8; args.ebx |= ACER_AMW0_WIRELESS_MASK; status = wmab_execute(&args, &out); if (ACPI_FAILURE(status)) return status; obj = (union acpi_object *) out.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(struct wmab_ret)) { ret = *((struct wmab_ret *) obj->buffer.pointer); } else { return AE_ERROR; } if (ret.eax & 0x1) interface->capability |= ACER_CAP_WIRELESS; args.ebx = 2 << 8; args.ebx |= ACER_AMW0_BLUETOOTH_MASK; status = wmab_execute(&args, &out); if (ACPI_FAILURE(status)) return status; obj = (union acpi_object *) out.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(struct wmab_ret)) { ret = *((struct wmab_ret *) obj->buffer.pointer); } else { return AE_ERROR; } if (ret.eax & 0x1) interface->capability |= ACER_CAP_BLUETOOTH; kfree(out.pointer); /* * This appears to be safe to enable, since all Wistron based laptops * appear to use the same EC register for brightness, even if they * differ for wireless, etc */ if (quirks->brightness >= 0) interface->capability |= ACER_CAP_BRIGHTNESS; return AE_OK; } static struct wmi_interface AMW0_interface = { .type = ACER_AMW0, }; static struct wmi_interface AMW0_V2_interface = { .type = ACER_AMW0_V2, }; /* * New interface (The WMID interface) */ static acpi_status WMI_execute_u32(u32 method_id, u32 in, u32 *out) { struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) }; struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL }; union acpi_object *obj; u32 tmp; acpi_status status; status = wmi_evaluate_method(WMID_GUID1, 1, method_id, &input, &result); if (ACPI_FAILURE(status)) return status; obj = (union acpi_object *) result.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) { tmp = *((u32 *) obj->buffer.pointer); } else { tmp = 0; } if (out) *out = tmp; kfree(result.pointer); return status; } static acpi_status WMID_get_u32(u32 *value, u32 cap, struct wmi_interface *iface) { acpi_status status; u8 tmp; u32 result, method_id = 0; switch (cap) { case ACER_CAP_WIRELESS: method_id = ACER_WMID_GET_WIRELESS_METHODID; break; case ACER_CAP_BLUETOOTH: method_id = ACER_WMID_GET_BLUETOOTH_METHODID; break; case ACER_CAP_BRIGHTNESS: method_id = ACER_WMID_GET_BRIGHTNESS_METHODID; break; case ACER_CAP_THREEG: method_id = ACER_WMID_GET_THREEG_METHODID; break; case ACER_CAP_MAILLED: if (quirks->mailled == 1) { ec_read(0x9f, &tmp); *value = tmp & 0x1; return 0; } default: return AE_ERROR; } status = WMI_execute_u32(method_id, 0, &result); if (ACPI_SUCCESS(status)) *value = (u8)result; return status; } static acpi_status WMID_set_u32(u32 value, u32 cap, struct wmi_interface *iface) { u32 method_id = 0; char param; switch (cap) { case ACER_CAP_BRIGHTNESS: if (value > max_brightness) return AE_BAD_PARAMETER; method_id = ACER_WMID_SET_BRIGHTNESS_METHODID; break; case ACER_CAP_WIRELESS: if (value > 1) return AE_BAD_PARAMETER; method_id = ACER_WMID_SET_WIRELESS_METHODID; break; case ACER_CAP_BLUETOOTH: if (value > 1) return AE_BAD_PARAMETER; method_id = ACER_WMID_SET_BLUETOOTH_METHODID; break; case ACER_CAP_THREEG: if (value > 1) return AE_BAD_PARAMETER; method_id = ACER_WMID_SET_THREEG_METHODID; break; case ACER_CAP_MAILLED: if (value > 1) return AE_BAD_PARAMETER; if (quirks->mailled == 1) { param = value ? 0x92 : 0x93; i8042_command(&param, 0x1059); return 0; } break; default: return AE_ERROR; } return WMI_execute_u32(method_id, (u32)value, NULL); } static acpi_status WMID_set_capabilities(void) { struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL}; union acpi_object *obj; acpi_status status; u32 devices; status = wmi_query_block(WMID_GUID2, 1, &out); if (ACPI_FAILURE(status)) return status; obj = (union acpi_object *) out.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) { devices = *((u32 *) obj->buffer.pointer); } else { return AE_ERROR; } /* Not sure on the meaning of the relevant bits yet to detect these */ interface->capability |= ACER_CAP_WIRELESS; interface->capability |= ACER_CAP_THREEG; /* WMID always provides brightness methods */ interface->capability |= ACER_CAP_BRIGHTNESS; if (devices & 0x10) interface->capability |= ACER_CAP_BLUETOOTH; if (!(devices & 0x20)) max_brightness = 0x9; return status; } static struct wmi_interface wmid_interface = { .type = ACER_WMID, }; /* * Generic Device (interface-independent) */ static acpi_status get_u32(u32 *value, u32 cap) { acpi_status status = AE_ERROR; switch (interface->type) { case ACER_AMW0: status = AMW0_get_u32(value, cap, interface); break; case ACER_AMW0_V2: if (cap == ACER_CAP_MAILLED) { status = AMW0_get_u32(value, cap, interface); break; } case ACER_WMID: status = WMID_get_u32(value, cap, interface); break; } return status; } static acpi_status set_u32(u32 value, u32 cap) { acpi_status status; if (interface->capability & cap) { switch (interface->type) { case ACER_AMW0: return AMW0_set_u32(value, cap, interface); case ACER_AMW0_V2: if (cap == ACER_CAP_MAILLED) return AMW0_set_u32(value, cap, interface); /* * On some models, some WMID methods don't toggle * properly. For those cases, we want to run the AMW0 * method afterwards to be certain we've really toggled * the device state. */ if (cap == ACER_CAP_WIRELESS || cap == ACER_CAP_BLUETOOTH) { status = WMID_set_u32(value, cap, interface); if (ACPI_FAILURE(status)) return status; return AMW0_set_u32(value, cap, interface); } case ACER_WMID: return WMID_set_u32(value, cap, interface); default: return AE_BAD_PARAMETER; } } return AE_BAD_PARAMETER; } static void __init acer_commandline_init(void) { /* * These will all fail silently if the value given is invalid, or the * capability isn't available on the given interface */ set_u32(mailled, ACER_CAP_MAILLED); set_u32(threeg, ACER_CAP_THREEG); set_u32(brightness, ACER_CAP_BRIGHTNESS); } /* * LED device (Mail LED only, no other LEDs known yet) */ static void mail_led_set(struct led_classdev *led_cdev, enum led_brightness value) { set_u32(value, ACER_CAP_MAILLED); } static struct led_classdev mail_led = { .name = "acer-wmi::mail", .brightness_set = mail_led_set, }; static int __devinit acer_led_init(struct device *dev) { return led_classdev_register(dev, &mail_led); } static void acer_led_exit(void) { led_classdev_unregister(&mail_led); } /* * Backlight device */ static struct backlight_device *acer_backlight_device; static int read_brightness(struct backlight_device *bd) { u32 value; get_u32(&value, ACER_CAP_BRIGHTNESS); return value; } static int update_bl_status(struct backlight_device *bd) { int intensity = bd->props.brightness; if (bd->props.power != FB_BLANK_UNBLANK) intensity = 0; if (bd->props.fb_blank != FB_BLANK_UNBLANK) intensity = 0; set_u32(intensity, ACER_CAP_BRIGHTNESS); return 0; } static struct backlight_ops acer_bl_ops = { .get_brightness = read_brightness, .update_status = update_bl_status, }; static int __devinit acer_backlight_init(struct device *dev) { struct backlight_device *bd; bd = backlight_device_register("acer-wmi", dev, NULL, &acer_bl_ops); if (IS_ERR(bd)) { printk(ACER_ERR "Could not register Acer backlight device\n"); acer_backlight_device = NULL; return PTR_ERR(bd); } acer_backlight_device = bd; bd->props.power = FB_BLANK_UNBLANK; bd->props.brightness = max_brightness; bd->props.max_brightness = max_brightness; backlight_update_status(bd); return 0; } static void acer_backlight_exit(void) { backlight_device_unregister(acer_backlight_device); } /* * Rfkill devices */ static void acer_rfkill_update(struct work_struct *ignored); static DECLARE_DELAYED_WORK(acer_rfkill_work, acer_rfkill_update); static void acer_rfkill_update(struct work_struct *ignored) { u32 state; acpi_status status; status = get_u32(&state, ACER_CAP_WIRELESS); if (ACPI_SUCCESS(status)) rfkill_force_state(wireless_rfkill, state ? RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED); if (has_cap(ACER_CAP_BLUETOOTH)) { status = get_u32(&state, ACER_CAP_BLUETOOTH); if (ACPI_SUCCESS(status)) rfkill_force_state(bluetooth_rfkill, state ? RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED); } schedule_delayed_work(&acer_rfkill_work, round_jiffies_relative(HZ)); } static int acer_rfkill_set(void *data, enum rfkill_state state) { acpi_status status; u32 *cap = data; status = set_u32((u32) (state == RFKILL_STATE_UNBLOCKED), *cap); if (ACPI_FAILURE(status)) return -ENODEV; return 0; } static struct rfkill * acer_rfkill_register(struct device *dev, enum rfkill_type type, char *name, u32 cap) { int err; u32 state; u32 *data; struct rfkill *rfkill_dev; rfkill_dev = rfkill_allocate(dev, type); if (!rfkill_dev) return ERR_PTR(-ENOMEM); rfkill_dev->name = name; get_u32(&state, cap); rfkill_dev->state = state ? RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED; data = kzalloc(sizeof(u32), GFP_KERNEL); if (!data) { rfkill_free(rfkill_dev); return ERR_PTR(-ENOMEM); } *data = cap; rfkill_dev->data = data; rfkill_dev->toggle_radio = acer_rfkill_set; rfkill_dev->user_claim_unsupported = 1; err = rfkill_register(rfkill_dev); if (err) { kfree(rfkill_dev->data); rfkill_free(rfkill_dev); return ERR_PTR(err); } return rfkill_dev; } static int acer_rfkill_init(struct device *dev) { wireless_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_WLAN, "acer-wireless", ACER_CAP_WIRELESS); if (IS_ERR(wireless_rfkill)) return PTR_ERR(wireless_rfkill); if (has_cap(ACER_CAP_BLUETOOTH)) { bluetooth_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_BLUETOOTH, "acer-bluetooth", ACER_CAP_BLUETOOTH); if (IS_ERR(bluetooth_rfkill)) { kfree(wireless_rfkill->data); rfkill_unregister(wireless_rfkill); return PTR_ERR(bluetooth_rfkill); } } schedule_delayed_work(&acer_rfkill_work, round_jiffies_relative(HZ)); return 0; } static void acer_rfkill_exit(void) { cancel_delayed_work_sync(&acer_rfkill_work); kfree(wireless_rfkill->data); rfkill_unregister(wireless_rfkill); if (has_cap(ACER_CAP_BLUETOOTH)) { kfree(wireless_rfkill->data); rfkill_unregister(bluetooth_rfkill); } return; } /* * sysfs interface */ static ssize_t show_bool_threeg(struct device *dev, struct device_attribute *attr, char *buf) { u32 result; \ acpi_status status = get_u32(&result, ACER_CAP_THREEG); if (ACPI_SUCCESS(status)) return sprintf(buf, "%u\n", result); return sprintf(buf, "Read error\n"); } static ssize_t set_bool_threeg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { u32 tmp = simple_strtoul(buf, NULL, 10); acpi_status status = set_u32(tmp, ACER_CAP_THREEG); if (ACPI_FAILURE(status)) return -EINVAL; return count; } static DEVICE_ATTR(threeg, S_IWUGO | S_IRUGO | S_IWUSR, show_bool_threeg, set_bool_threeg); static ssize_t show_interface(struct device *dev, struct device_attribute *attr, char *buf) { switch (interface->type) { case ACER_AMW0: return sprintf(buf, "AMW0\n"); case ACER_AMW0_V2: return sprintf(buf, "AMW0 v2\n"); case ACER_WMID: return sprintf(buf, "WMID\n"); default: return sprintf(buf, "Error!\n"); } } static DEVICE_ATTR(interface, S_IWUGO | S_IRUGO | S_IWUSR, show_interface, NULL); /* * debugfs functions */ static u32 get_wmid_devices(void) { struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL}; union acpi_object *obj; acpi_status status; status = wmi_query_block(WMID_GUID2, 1, &out); if (ACPI_FAILURE(status)) return 0; obj = (union acpi_object *) out.pointer; if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) { return *((u32 *) obj->buffer.pointer); } else { return 0; } } /* * Platform device */ static int __devinit acer_platform_probe(struct platform_device *device) { int err; if (has_cap(ACER_CAP_MAILLED)) { err = acer_led_init(&device->dev); if (err) goto error_mailled; } if (has_cap(ACER_CAP_BRIGHTNESS)) { err = acer_backlight_init(&device->dev); if (err) goto error_brightness; } err = acer_rfkill_init(&device->dev); return err; error_brightness: acer_led_exit(); error_mailled: return err; } static int acer_platform_remove(struct platform_device *device) { if (has_cap(ACER_CAP_MAILLED)) acer_led_exit(); if (has_cap(ACER_CAP_BRIGHTNESS)) acer_backlight_exit(); acer_rfkill_exit(); return 0; } static int acer_platform_suspend(struct platform_device *dev, pm_message_t state) { u32 value; struct acer_data *data = &interface->data; if (!data) return -ENOMEM; if (has_cap(ACER_CAP_MAILLED)) { get_u32(&value, ACER_CAP_MAILLED); data->mailled = value; } if (has_cap(ACER_CAP_BRIGHTNESS)) { get_u32(&value, ACER_CAP_BRIGHTNESS); data->brightness = value; } return 0; } static int acer_platform_resume(struct platform_device *device) { struct acer_data *data = &interface->data; if (!data) return -ENOMEM; if (has_cap(ACER_CAP_MAILLED)) set_u32(data->mailled, ACER_CAP_MAILLED); if (has_cap(ACER_CAP_BRIGHTNESS)) set_u32(data->brightness, ACER_CAP_BRIGHTNESS); return 0; } static struct platform_driver acer_platform_driver = { .driver = { .name = "acer-wmi", .owner = THIS_MODULE, }, .probe = acer_platform_probe, .remove = acer_platform_remove, .suspend = acer_platform_suspend, .resume = acer_platform_resume, }; static struct platform_device *acer_platform_device; static int remove_sysfs(struct platform_device *device) { if (has_cap(ACER_CAP_THREEG)) device_remove_file(&device->dev, &dev_attr_threeg); device_remove_file(&device->dev, &dev_attr_interface); return 0; } static int create_sysfs(void) { int retval = -ENOMEM; if (has_cap(ACER_CAP_THREEG)) { retval = device_create_file(&acer_platform_device->dev, &dev_attr_threeg); if (retval) goto error_sysfs; } retval = device_create_file(&acer_platform_device->dev, &dev_attr_interface); if (retval) goto error_sysfs; return 0; error_sysfs: remove_sysfs(acer_platform_device); return retval; } static void remove_debugfs(void) { debugfs_remove(interface->debug.devices); debugfs_remove(interface->debug.root); } static int create_debugfs(void) { interface->debug.root = debugfs_create_dir("acer-wmi", NULL); if (!interface->debug.root) { printk(ACER_ERR "Failed to create debugfs directory"); return -ENOMEM; } interface->debug.devices = debugfs_create_u32("devices", S_IRUGO, interface->debug.root, &interface->debug.wmid_devices); if (!interface->debug.devices) goto error_debugfs; return 0; error_debugfs: remove_debugfs(); return -ENOMEM; } static int __init acer_wmi_init(void) { int err; printk(ACER_INFO "Acer Laptop ACPI-WMI Extras\n"); find_quirks(); /* * Detect which ACPI-WMI interface we're using. */ if (wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1)) interface = &AMW0_V2_interface; if (!wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1)) interface = &wmid_interface; if (wmi_has_guid(WMID_GUID2) && interface) { if (ACPI_FAILURE(WMID_set_capabilities())) { printk(ACER_ERR "Unable to detect available WMID " "devices\n"); return -ENODEV; } } else if (!wmi_has_guid(WMID_GUID2) && interface) { printk(ACER_ERR "No WMID device detection method found\n"); return -ENODEV; } if (wmi_has_guid(AMW0_GUID1) && !wmi_has_guid(WMID_GUID1)) { interface = &AMW0_interface; if (ACPI_FAILURE(AMW0_set_capabilities())) { printk(ACER_ERR "Unable to detect available AMW0 " "devices\n"); return -ENODEV; } } if (wmi_has_guid(AMW0_GUID1)) AMW0_find_mailled(); if (!interface) { printk(ACER_ERR "No or unsupported WMI interface, unable to " "load\n"); return -ENODEV; } set_quirks(); if (!acpi_video_backlight_support() && has_cap(ACER_CAP_BRIGHTNESS)) { interface->capability &= ~ACER_CAP_BRIGHTNESS; printk(ACER_INFO "Brightness must be controlled by " "generic video driver\n"); } if (platform_driver_register(&acer_platform_driver)) { printk(ACER_ERR "Unable to register platform driver.\n"); goto error_platform_register; } acer_platform_device = platform_device_alloc("acer-wmi", -1); platform_device_add(acer_platform_device); err = create_sysfs(); if (err) return err; if (wmi_has_guid(WMID_GUID2)) { interface->debug.wmid_devices = get_wmid_devices(); err = create_debugfs(); if (err) return err; } /* Override any initial settings with values from the commandline */ acer_commandline_init(); return 0; error_platform_register: return -ENODEV; } static void __exit acer_wmi_exit(void) { remove_sysfs(acer_platform_device); remove_debugfs(); platform_device_del(acer_platform_device); platform_driver_unregister(&acer_platform_driver); printk(ACER_INFO "Acer Laptop WMI Extras unloaded\n"); return; } module_init(acer_wmi_init); module_exit(acer_wmi_exit);