Commit eaa85689 authored by Linus Torvalds's avatar Linus Torvalds

Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/w1-2.6

* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/w1-2.6:
  [PATCH] w1: warning fix
  [PATCH] w1: clean up W1_CON dependency.
  [PATCH] drivers/w1/w1.c: fix a compile error
  [PATCH] W1: fix dependencies of W1_SLAVE_DS2433_CRC
  [PATCH] W1: possible cleanups
  [PATCH] W1: cleanups
  [PATCH] w1 exports
  [PATCH] w1: Use mutexes instead of semaphores.
  [PATCH] w1: Make w1 connector notifications depend on connector.
  [PATCH] w1: netlink: Mark netlink group 1 as unused.
  [PATCH] w1: Move w1-connector definitions into linux/include/connector.h
  [PATCH] w1: Userspace communication protocol over connector.
  [PATCH] w1: Replace dscore and ds_w1_bridge with ds2490 driver.
  [PATCH] w1: Added default generic read/write operations.
parents 6c763eb9 c6976a4e
Kernel driver ds2490
====================
Supported chips:
* Maxim DS2490 based
Author: Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Description
-----------
The Maixm/Dallas Semiconductor DS2490 is a chip
which allows to build USB <-> W1 bridges.
DS9490(R) is a USB <-> W1 bus master device
which has 0x81 family ID integrated chip and DS2490
low-level operational chip.
......@@ -27,8 +27,19 @@ When a w1 master driver registers with the w1 subsystem, the following occurs:
When a device is found on the bus, w1 core checks if driver for it's family is
loaded. If so, the family driver is attached to the slave.
If there is no driver for the family, a simple sysfs entry is created
for the slave device.
If there is no driver for the family, default one is assigned, which allows to perform
almost any kind of operations. Each logical operation is a transaction
in nature, which can contain several (two or one) low-level operations.
Let's see how one can read EEPROM context:
1. one must write control buffer, i.e. buffer containing command byte
and two byte address. At this step bus is reset and appropriate device
is selected using either W1_SKIP_ROM or W1_MATCH_ROM command.
Then provided control buffer is being written to the wire.
2. reading. This will issue reading eeprom response.
It is possible that between 1. and 2. w1 master thread will reset bus for searching
and slave device will be even removed, but in this case 0xff will
be read, since no device was selected.
W1 device families
......@@ -89,4 +100,5 @@ driver - (standard) symlink to the w1 driver
name - the device name, usually the same as the directory name
w1_slave - (optional) a binary file whose meaning depends on the
family driver
rw - (optional) created for slave devices which do not have
appropriate family driver. Allows to read/write binary data.
Userspace communication protocol over connector [1].
Message types.
=============
There are three types of messages between w1 core and userspace:
1. Events. They are generated each time new master or slave device found
either due to automatic or requested search.
2. Userspace commands. Includes read/write and search/alarm search comamnds.
3. Replies to userspace commands.
Protocol.
========
[struct cn_msg] - connector header. It's length field is equal to size of the attached data.
[struct w1_netlink_msg] - w1 netlink header.
__u8 type - message type.
W1_SLAVE_ADD/W1_SLAVE_REMOVE - slave add/remove events.
W1_MASTER_ADD/W1_MASTER_REMOVE - master add/remove events.
W1_MASTER_CMD - userspace command for bus master device (search/alarm search).
W1_SLAVE_CMD - userspace command for slave device (read/write/ search/alarm search
for bus master device where given slave device found).
__u8 res - reserved
__u16 len - size of attached to this header data.
union {
__u8 id; - slave unique device id
struct w1_mst {
__u32 id; - master's id.
__u32 res; - reserved
} mst;
} id;
[strucrt w1_netlink_cmd] - command for gived master or slave device.
__u8 cmd - command opcode.
W1_CMD_READ - read command.
W1_CMD_WRITE - write command.
W1_CMD_SEARCH - search command.
W1_CMD_ALARM_SEARCH - alarm search command.
__u8 res - reserved
__u16 len - length of data for this command.
For read command data must be allocated like for write command.
__u8 data[0] - data for this command.
Each connector message can include one or more w1_netlink_msg with zero of more attached w1_netlink_cmd messages.
For event messages there are no w1_netlink_cmd embedded structures, only connector header
and w1_netlink_msg strucutre with "len" field being zero and filled type (one of event types)
and id - either 8 bytes of slave unique id in host order, or master's id, which is assigned
to bus master device when it is added to w1 core.
Currently replies to userspace commands are only generated for read command request.
One reply is generated exactly for one w1_netlink_cmd read request.
Replies are not combined when sent - i.e. typical reply messages looks like the following:
[cn_msg][w1_netlink_msg][w1_netlink_cmd]
cn_msg.len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
w1_netlink_msg.len = sizeof(struct w1_netlink_cmd) + cmd->len;
w1_netlink_cmd.len = cmd->len;
Operation steps in w1 core when new command is received.
=======================================================
When new message (w1_netlink_msg) is received w1 core detects if it is master of slave request,
according to w1_netlink_msg.type field.
Then master or slave device is searched for.
When found, master device (requested or those one on where slave device is found) is locked.
If slave command is requested, then reset/select procedure is started to select given device.
Then all requested in w1_netlink_msg operations are performed one by one.
If command requires reply (like read command) it is sent on command completion.
When all commands (w1_netlink_cmd) are processed muster device is unlocked
and next w1_netlink_msg header processing started.
Connector [1] specific documentation.
====================================
Each connector message includes two u32 fields as "address".
w1 uses CN_W1_IDX and CN_W1_VAL defined in include/linux/connector.h header.
Each message also includes sequence and acknowledge numbers.
Sequence number for event messages is appropriate bus master sequence number increased with
each event message sent "through" this master.
Sequence number for userspace requests is set by userspace application.
Sequence number for reply is the same as was in request, and
acknowledge number is set to seq+1.
Additional documantion, source code examples.
============================================
1. Documentation/connector
2. http://tservice.net.ru/~s0mbre/archive/w1
This archive includes userspace application w1d.c which
uses read/write/search commands for all master/slave devices found on the bus.
......@@ -3,7 +3,7 @@ menu "Dallas's 1-wire bus"
config W1
tristate "Dallas's 1-wire support"
---help---
Dallas's 1-wire bus is useful to connect slow 1-pin devices
Dallas' 1-wire bus is useful to connect slow 1-pin devices
such as iButtons and thermal sensors.
If you want W1 support, you should say Y here.
......@@ -11,6 +11,18 @@ config W1
This W1 support can also be built as a module. If so, the module
will be called wire.ko.
config W1_CON
depends on CONNECTOR && W1
bool "Userspace communication over connector"
default y
--- help ---
This allows to communicate with userspace using connector [Documentation/connector].
There are three types of messages between w1 core and userspace:
1. Events. They are generated each time new master or slave device found
either due to automatic or requested search.
2. Userspace commands. Includes read/write and search/alarm search comamnds.
3. Replies to userspace commands.
source drivers/w1/masters/Kconfig
source drivers/w1/slaves/Kconfig
......
......@@ -2,10 +2,6 @@
# Makefile for the Dallas's 1-wire bus.
#
ifneq ($(CONFIG_NET), y)
EXTRA_CFLAGS += -DNETLINK_DISABLED
endif
ifeq ($(CONFIG_W1_DS2433_CRC), y)
EXTRA_CFLAGS += -DCONFIG_W1_F23_CRC
endif
......
......@@ -15,24 +15,15 @@ config W1_MASTER_MATROX
This support is also available as a module. If so, the module
will be called matrox_w1.ko.
config W1_MASTER_DS9490
tristate "DS9490R transport layer driver"
depends on W1 && USB
help
Say Y here if you want to have a driver for DS9490R UWB <-> W1 bridge.
This support is also available as a module. If so, the module
will be called ds9490r.ko.
config W1_MASTER_DS9490_BRIDGE
tristate "DS9490R USB <-> W1 transport layer for 1-wire"
depends on W1_MASTER_DS9490
help
Say Y here if you want to communicate with your 1-wire devices
using DS9490R USB bridge.
This support is also available as a module. If so, the module
will be called ds_w1_bridge.ko.
config W1_MASTER_DS2490
tristate "DS2490 USB <-> W1 transport layer for 1-wire"
depends on W1 && USB
help
Say Y here if you want to have a driver for DS2490 based USB <-> W1 bridges,
for example DS9490*.
This support is also available as a module. If so, the module
will be called ds2490.ko.
config W1_MASTER_DS2482
tristate "Maxim DS2482 I2C to 1-Wire bridge"
......
......@@ -3,11 +3,6 @@
#
obj-$(CONFIG_W1_MASTER_MATROX) += matrox_w1.o
obj-$(CONFIG_W1_MASTER_DS9490) += ds9490r.o
ds9490r-objs := dscore.o
obj-$(CONFIG_W1_MASTER_DS9490_BRIDGE) += ds_w1_bridge.o
obj-$(CONFIG_W1_MASTER_DS2490) += ds2490.o
obj-$(CONFIG_W1_MASTER_DS2482) += ds2482.o
......@@ -125,7 +125,7 @@ struct ds2482_w1_chan {
struct ds2482_data {
struct i2c_client client;
struct semaphore access_lock;
struct mutex access_lock;
/* 1-wire interface(s) */
int w1_count; /* 1 or 8 */
......@@ -265,7 +265,7 @@ static u8 ds2482_w1_touch_bit(void *data, u8 bit)
struct ds2482_data *pdev = pchan->pdev;
int status = -1;
down(&pdev->access_lock);
mutex_lock(&pdev->access_lock);
/* Select the channel */
ds2482_wait_1wire_idle(pdev);
......@@ -277,7 +277,7 @@ static u8 ds2482_w1_touch_bit(void *data, u8 bit)
bit ? 0xFF : 0))
status = ds2482_wait_1wire_idle(pdev);
up(&pdev->access_lock);
mutex_unlock(&pdev->access_lock);
return (status & DS2482_REG_STS_SBR) ? 1 : 0;
}
......@@ -297,7 +297,7 @@ static u8 ds2482_w1_triplet(void *data, u8 dbit)
struct ds2482_data *pdev = pchan->pdev;
int status = (3 << 5);
down(&pdev->access_lock);
mutex_lock(&pdev->access_lock);
/* Select the channel */
ds2482_wait_1wire_idle(pdev);
......@@ -309,7 +309,7 @@ static u8 ds2482_w1_triplet(void *data, u8 dbit)
dbit ? 0xFF : 0))
status = ds2482_wait_1wire_idle(pdev);
up(&pdev->access_lock);
mutex_unlock(&pdev->access_lock);
/* Decode the status */
return (status >> 5);
......@@ -326,7 +326,7 @@ static void ds2482_w1_write_byte(void *data, u8 byte)
struct ds2482_w1_chan *pchan = data;
struct ds2482_data *pdev = pchan->pdev;
down(&pdev->access_lock);
mutex_lock(&pdev->access_lock);
/* Select the channel */
ds2482_wait_1wire_idle(pdev);
......@@ -336,7 +336,7 @@ static void ds2482_w1_write_byte(void *data, u8 byte)
/* Send the write byte command */
ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
up(&pdev->access_lock);
mutex_unlock(&pdev->access_lock);
}
/**
......@@ -351,7 +351,7 @@ static u8 ds2482_w1_read_byte(void *data)
struct ds2482_data *pdev = pchan->pdev;
int result;
down(&pdev->access_lock);
mutex_lock(&pdev->access_lock);
/* Select the channel */
ds2482_wait_1wire_idle(pdev);
......@@ -370,7 +370,7 @@ static u8 ds2482_w1_read_byte(void *data)
/* Read the data byte */
result = i2c_smbus_read_byte(&pdev->client);
up(&pdev->access_lock);
mutex_unlock(&pdev->access_lock);
return result;
}
......@@ -389,7 +389,7 @@ static u8 ds2482_w1_reset_bus(void *data)
int err;
u8 retval = 1;
down(&pdev->access_lock);
mutex_lock(&pdev->access_lock);
/* Select the channel */
ds2482_wait_1wire_idle(pdev);
......@@ -409,7 +409,7 @@ static u8 ds2482_w1_reset_bus(void *data)
0xF0);
}
up(&pdev->access_lock);
mutex_unlock(&pdev->access_lock);
return retval;
}
......@@ -482,7 +482,7 @@ static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind)
snprintf(new_client->name, sizeof(new_client->name), "ds2482-%d00",
data->w1_count);
init_MUTEX(&data->access_lock);
mutex_init(&data->access_lock);
/* Tell the I2C layer a new client has arrived */
if ((err = i2c_attach_client(new_client)))
......
/*
* ds_w1_bridge.c
*
* Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
*
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/types.h>
#include "../w1.h"
#include "../w1_int.h"
#include "dscore.h"
static struct ds_device *ds_dev;
static struct w1_bus_master *ds_bus_master;
static u8 ds9490r_touch_bit(void *data, u8 bit)
{
u8 ret;
struct ds_device *dev = data;
if (ds_touch_bit(dev, bit, &ret))
return 0;
return ret;
}
static void ds9490r_write_bit(void *data, u8 bit)
{
struct ds_device *dev = data;
ds_write_bit(dev, bit);
}
static void ds9490r_write_byte(void *data, u8 byte)
{
struct ds_device *dev = data;
ds_write_byte(dev, byte);
}
static u8 ds9490r_read_bit(void *data)
{
struct ds_device *dev = data;
int err;
u8 bit = 0;
err = ds_touch_bit(dev, 1, &bit);
if (err)
return 0;
//err = ds_read_bit(dev, &bit);
//if (err)
// return 0;
return bit & 1;
}
static u8 ds9490r_read_byte(void *data)
{
struct ds_device *dev = data;
int err;
u8 byte = 0;
err = ds_read_byte(dev, &byte);
if (err)
return 0;
return byte;
}
static void ds9490r_write_block(void *data, const u8 *buf, int len)
{
struct ds_device *dev = data;
ds_write_block(dev, (u8 *)buf, len);
}
static u8 ds9490r_read_block(void *data, u8 *buf, int len)
{
struct ds_device *dev = data;
int err;
err = ds_read_block(dev, buf, len);
if (err < 0)
return 0;
return len;
}
static u8 ds9490r_reset(void *data)
{
struct ds_device *dev = data;
struct ds_status st;
int err;
memset(&st, 0, sizeof(st));
err = ds_reset(dev, &st);
if (err)
return 1;
return 0;
}
static int __devinit ds_w1_init(void)
{
int err;
ds_bus_master = kmalloc(sizeof(*ds_bus_master), GFP_KERNEL);
if (!ds_bus_master) {
printk(KERN_ERR "Failed to allocate DS9490R USB<->W1 bus_master structure.\n");
return -ENOMEM;
}
ds_dev = ds_get_device();
if (!ds_dev) {
printk(KERN_ERR "DS9490R is not registered.\n");
err = -ENODEV;
goto err_out_free_bus_master;
}
memset(ds_bus_master, 0, sizeof(*ds_bus_master));
ds_bus_master->data = ds_dev;
ds_bus_master->touch_bit = &ds9490r_touch_bit;
ds_bus_master->read_bit = &ds9490r_read_bit;
ds_bus_master->write_bit = &ds9490r_write_bit;
ds_bus_master->read_byte = &ds9490r_read_byte;
ds_bus_master->write_byte = &ds9490r_write_byte;
ds_bus_master->read_block = &ds9490r_read_block;
ds_bus_master->write_block = &ds9490r_write_block;
ds_bus_master->reset_bus = &ds9490r_reset;
err = w1_add_master_device(ds_bus_master);
if (err)
goto err_out_put_device;
return 0;
err_out_put_device:
ds_put_device(ds_dev);
err_out_free_bus_master:
kfree(ds_bus_master);
return err;
}
static void __devexit ds_w1_fini(void)
{
w1_remove_master_device(ds_bus_master);
ds_put_device(ds_dev);
kfree(ds_bus_master);
}
module_init(ds_w1_init);
module_exit(ds_w1_fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
/*
* dscore.h
*
* Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
*
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __DSCORE_H
#define __DSCORE_H
#include <linux/usb.h>
#include <asm/atomic.h>
/* COMMAND TYPE CODES */
#define CONTROL_CMD 0x00
#define COMM_CMD 0x01
#define MODE_CMD 0x02
/* CONTROL COMMAND CODES */
#define CTL_RESET_DEVICE 0x0000
#define CTL_START_EXE 0x0001
#define CTL_RESUME_EXE 0x0002
#define CTL_HALT_EXE_IDLE 0x0003
#define CTL_HALT_EXE_DONE 0x0004
#define CTL_FLUSH_COMM_CMDS 0x0007
#define CTL_FLUSH_RCV_BUFFER 0x0008
#define CTL_FLUSH_XMT_BUFFER 0x0009
#define CTL_GET_COMM_CMDS 0x000A
/* MODE COMMAND CODES */
#define MOD_PULSE_EN 0x0000
#define MOD_SPEED_CHANGE_EN 0x0001
#define MOD_1WIRE_SPEED 0x0002
#define MOD_STRONG_PU_DURATION 0x0003
#define MOD_PULLDOWN_SLEWRATE 0x0004
#define MOD_PROG_PULSE_DURATION 0x0005
#define MOD_WRITE1_LOWTIME 0x0006
#define MOD_DSOW0_TREC 0x0007
/* COMMUNICATION COMMAND CODES */
#define COMM_ERROR_ESCAPE 0x0601
#define COMM_SET_DURATION 0x0012
#define COMM_BIT_IO 0x0020
#define COMM_PULSE 0x0030
#define COMM_1_WIRE_RESET 0x0042
#define COMM_BYTE_IO 0x0052
#define COMM_MATCH_ACCESS 0x0064
#define COMM_BLOCK_IO 0x0074
#define COMM_READ_STRAIGHT 0x0080
#define COMM_DO_RELEASE 0x6092
#define COMM_SET_PATH 0x00A2
#define COMM_WRITE_SRAM_PAGE 0x00B2
#define COMM_WRITE_EPROM 0x00C4
#define COMM_READ_CRC_PROT_PAGE 0x00D4
#define COMM_READ_REDIRECT_PAGE_CRC 0x21E4
#define COMM_SEARCH_ACCESS 0x00F4
/* Communication command bits */
#define COMM_TYPE 0x0008
#define COMM_SE 0x0008
#define COMM_D 0x0008
#define COMM_Z 0x0008
#define COMM_CH 0x0008
#define COMM_SM 0x0008
#define COMM_R 0x0008
#define COMM_IM 0x0001
#define COMM_PS 0x4000
#define COMM_PST 0x4000
#define COMM_CIB 0x4000
#define COMM_RTS 0x4000
#define COMM_DT 0x2000
#define COMM_SPU 0x1000
#define COMM_F 0x0800
#define COMM_NTP 0x0400
#define COMM_ICP 0x0200
#define COMM_RST 0x0100
#define PULSE_PROG 0x01
#define PULSE_SPUE 0x02
#define BRANCH_MAIN 0xCC
#define BRANCH_AUX 0x33
/*
* Duration of the strong pull-up pulse in milliseconds.
*/
#define PULLUP_PULSE_DURATION 750
/* Status flags */
#define ST_SPUA 0x01 /* Strong Pull-up is active */
#define ST_PRGA 0x02 /* 12V programming pulse is being generated */
#define ST_12VP 0x04 /* external 12V programming voltage is present */
#define ST_PMOD 0x08 /* DS2490 powered from USB and external sources */
#define ST_HALT 0x10 /* DS2490 is currently halted */
#define ST_IDLE 0x20 /* DS2490 is currently idle */
#define ST_EPOF 0x80
#define SPEED_NORMAL 0x00
#define SPEED_FLEXIBLE 0x01
#define SPEED_OVERDRIVE 0x02
#define NUM_EP 4
#define EP_CONTROL 0
#define EP_STATUS 1
#define EP_DATA_OUT 2
#define EP_DATA_IN 3
struct ds_device
{
struct usb_device *udev;
struct usb_interface *intf;
int ep[NUM_EP];
atomic_t refcnt;
};
struct ds_status
{
u8 enable;
u8 speed;
u8 pullup_dur;
u8 ppuls_dur;
u8 pulldown_slew;
u8 write1_time;
u8 write0_time;
u8 reserved0;
u8 status;
u8 command0;
u8 command1;
u8 command_buffer_status;
u8 data_out_buffer_status;
u8 data_in_buffer_status;
u8 reserved1;
u8 reserved2;
};
int ds_touch_bit(struct ds_device *, u8, u8 *);
int ds_read_byte(struct ds_device *, u8 *);
int ds_read_bit(struct ds_device *, u8 *);
int ds_write_byte(struct ds_device *, u8);
int ds_write_bit(struct ds_device *, u8);
int ds_reset(struct ds_device *, struct ds_status *);
struct ds_device * ds_get_device(void);
void ds_put_device(struct ds_device *);
int ds_write_block(struct ds_device *, u8 *, int);
int ds_read_block(struct ds_device *, u8 *, int);
#endif /* __DSCORE_H */
......@@ -28,7 +28,7 @@ config W1_SLAVE_DS2433
config W1_SLAVE_DS2433_CRC
bool "Protect DS2433 data with a CRC16"
depends on W1_DS2433
depends on W1_SLAVE_DS2433
select CRC16
help
Say Y here to protect DS2433 data with a CRC16.
......
......@@ -22,7 +22,6 @@
#endif
#include "../w1.h"
#include "../w1_io.h"
#include "../w1_int.h"
#include "../w1_family.h"
......@@ -106,11 +105,7 @@ static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off,
if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
return 0;
atomic_inc(&sl->refcnt);
if (down_interruptible(&sl->master->mutex)) {
count = 0;
goto out_dec;
}
mutex_lock(&sl->master->mutex);
#ifdef CONFIG_W1_F23_CRC
......@@ -141,9 +136,7 @@ static ssize_t w1_f23_read_bin(struct kobject *kobj, char *buf, loff_t off,
#endif /* CONFIG_W1_F23_CRC */
out_up:
up(&sl->master->mutex);
out_dec:
atomic_dec(&sl->refcnt);
mutex_unlock(&sl->master->mutex);
return count;
}
......@@ -232,11 +225,7 @@ static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
}
#endif /* CONFIG_W1_F23_CRC */
atomic_inc(&sl->refcnt);
if (down_interruptible(&sl->master->mutex)) {
count = 0;
goto out_dec;
}
mutex_lock(&sl->master->mutex);
/* Can only write data to one page at a time */
idx = 0;
......@@ -254,9 +243,7 @@ static ssize_t w1_f23_write_bin(struct kobject *kobj, char *buf, loff_t off,
}
out_up:
up(&sl->master->mutex);
out_dec:
atomic_dec(&sl->refcnt);
mutex_unlock(&sl->master->mutex);
return count;
}
......
......@@ -28,7 +28,6 @@
#include <linux/types.h>
#include "../w1.h"
#include "../w1_io.h"
#include "../w1_int.h"
#include "../w1_family.h"
......
......@@ -29,7 +29,6 @@
#include <linux/delay.h>
#include "../w1.h"
#include "../w1_io.h"
#include "../w1_int.h"
#include "../w1_family.h"
......@@ -166,12 +165,7 @@ static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, si
u8 rom[9], crc, verdict;
int i, max_trying = 10;
atomic_inc(&sl->refcnt);
smp_mb__after_atomic_inc();
if (down_interruptible(&sl->master->mutex)) {
count = 0;
goto out_dec;
}
mutex_lock(&sl->master->mutex);
if (off > W1_SLAVE_DATA_SIZE) {
count = 0;
......@@ -234,10 +228,7 @@ static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, si
count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom, sl->family->fid));
out:
up(&dev->mutex);
out_dec:
smp_mb__before_atomic_inc();
atomic_dec(&sl->refcnt);
mutex_unlock(&dev->mutex);
return count;
}
......
This diff is collapsed.
......@@ -41,10 +41,7 @@ struct w1_reg_num
#include <linux/completion.h>
#include <linux/device.h>
#include <net/sock.h>
#include <asm/semaphore.h>
#include <linux/mutex.h>
#include "w1_family.h"
......@@ -52,7 +49,7 @@ struct w1_reg_num
#define W1_SLAVE_DATA_SIZE 128
#define W1_SEARCH 0xF0
#define W1_CONDITIONAL_SEARCH 0xEC
#define W1_ALARM_SEARCH 0xEC
#define W1_CONVERT_TEMP 0x44
#define W1_SKIP_ROM 0xCC
#define W1_READ_SCRATCHPAD 0xBE
......@@ -60,7 +57,7 @@ struct w1_reg_num
#define W1_READ_PSUPPLY 0xB4
#define W1_MATCH_ROM 0x55
#define W1_SLAVE_ACTIVE (1<<0)
#define W1_SLAVE_ACTIVE 0
struct w1_slave
{
......@@ -145,8 +142,8 @@ struct w1_bus_master
*/
u8 (*reset_bus)(void *);
/** Really nice hardware can handles the ROM searches */
void (*search)(void *, w1_slave_found_callback);
/** Really nice hardware can handles the different types of ROM search */
void (*search)(void *, u8, w1_slave_found_callback);
};
#define W1_MASTER_NEED_EXIT 0
......@@ -173,19 +170,30 @@ struct w1_master
long flags;
struct task_struct *thread;
struct semaphore mutex;
struct mutex mutex;
struct device_driver *driver;
struct device dev;
struct w1_bus_master *bus_master;
u32 seq, groups;
struct sock *nls;
u32 seq;
};
int w1_create_master_attributes(struct w1_master *);
void w1_search(struct w1_master *dev, w1_slave_found_callback cb);
void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb);
void w1_search_devices(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb);
struct w1_slave *w1_search_slave(struct w1_reg_num *id);
void w1_search_process(struct w1_master *dev, u8 search_type);
struct w1_master *w1_search_master_id(u32 id);
u8 w1_triplet(struct w1_master *dev, int bdir);
void w1_write_8(struct w1_master *, u8);
int w1_reset_bus(struct w1_master *);
u8 w1_calc_crc8(u8 *, int);
void w1_write_block(struct w1_master *, const u8 *, int);
u8 w1_read_block(struct w1_master *, u8 *, int);
int w1_reset_select_slave(struct w1_slave *sl);
static inline struct w1_slave* dev_to_w1_slave(struct device *dev)
{
......@@ -202,15 +210,14 @@ static inline struct w1_master* dev_to_w1_master(struct device *dev)
return container_of(dev, struct w1_master, dev);
}
extern struct device_driver w1_master_driver;
extern struct device w1_master_device;
extern int w1_max_slave_count;
extern int w1_max_slave_ttl;
extern spinlock_t w1_mlock;
extern struct list_head w1_masters;
extern struct device_driver w1_master_driver;
extern struct device w1_master_device;
extern struct mutex w1_mlock;
int w1_process(void *data);
void w1_reconnect_slaves(struct w1_family *f);
extern int w1_process(void *);
#endif /* __KERNEL__ */
......
......@@ -107,6 +107,12 @@ struct w1_family * w1_family_registered(u8 fid)
return (ret) ? f : NULL;
}
static void __w1_family_put(struct w1_family *f)
{
if (atomic_dec_and_test(&f->refcnt))
f->need_exit = 1;
}
void w1_family_put(struct w1_family *f)
{
spin_lock(&w1_flock);
......@@ -114,19 +120,14 @@ void w1_family_put(struct w1_family *f)
spin_unlock(&w1_flock);
}
void __w1_family_put(struct w1_family *f)
{
if (atomic_dec_and_test(&f->refcnt))
f->need_exit = 1;
}
#if 0
void w1_family_get(struct w1_family *f)
{
spin_lock(&w1_flock);
__w1_family_get(f);
spin_unlock(&w1_flock);
}
#endif /* 0 */
void __w1_family_get(struct w1_family *f)
{
......@@ -135,8 +136,5 @@ void __w1_family_get(struct w1_family *f)
smp_mb__after_atomic_inc();
}
EXPORT_SYMBOL(w1_family_get);
EXPORT_SYMBOL(w1_family_put);
EXPORT_SYMBOL(w1_family_registered);
EXPORT_SYMBOL(w1_unregister_family);
EXPORT_SYMBOL(w1_register_family);
......@@ -57,12 +57,11 @@ struct w1_family
extern spinlock_t w1_flock;
void w1_family_get(struct w1_family *);
void w1_family_put(struct w1_family *);
void __w1_family_get(struct w1_family *);
void __w1_family_put(struct w1_family *);
struct w1_family * w1_family_registered(u8);
void w1_unregister_family(struct w1_family *);
int w1_register_family(struct w1_family *);
void w1_reconnect_slaves(struct w1_family *f);
#endif /* __W1_FAMILY_H */
......@@ -65,7 +65,7 @@ static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
atomic_set(&dev->refcnt, 2);
INIT_LIST_HEAD(&dev->slist);
init_MUTEX(&dev->mutex);
mutex_init(&dev->mutex);
memcpy(&dev->dev, device, sizeof(struct device));
snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
......@@ -74,16 +74,11 @@ static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
dev->driver = driver;
dev->groups = 1;
dev->seq = 1;
dev_init_netlink(dev);
err = device_register(&dev->dev);
if (err) {
printk(KERN_ERR "Failed to register master device. err=%d\n", err);
dev_fini_netlink(dev);
memset(dev, 0, sizeof(struct w1_master));
kfree(dev);
dev = NULL;
......@@ -131,12 +126,12 @@ int w1_add_master_device(struct w1_bus_master *master)
dev->initialized = 1;
spin_lock(&w1_mlock);
mutex_lock(&w1_mlock);
list_add(&dev->w1_master_entry, &w1_masters);
spin_unlock(&w1_mlock);
mutex_unlock(&w1_mlock);
memset(&msg, 0, sizeof(msg));
msg.id.mst.id = dev->id;
msg.id.mst.pid = dev->thread->pid;
msg.type = W1_MASTER_ADD;
w1_netlink_send(dev, &msg);
......@@ -153,7 +148,6 @@ int w1_add_master_device(struct w1_bus_master *master)
void __w1_remove_master_device(struct w1_master *dev)
{
struct w1_netlink_msg msg;
pid_t pid = dev->thread->pid;
set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
kthread_stop(dev->thread);
......@@ -166,8 +160,8 @@ void __w1_remove_master_device(struct w1_master *dev)
flush_signals(current);
}
memset(&msg, 0, sizeof(msg));
msg.id.mst.id = dev->id;
msg.id.mst.pid = pid;
msg.type = W1_MASTER_REMOVE;
w1_netlink_send(dev, &msg);
......
......@@ -23,10 +23,10 @@
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/module.h>
#include "w1.h"
#include "w1_log.h"
#include "w1_io.h"
static int w1_delay_parm = 1;
module_param_named(delay_coef, w1_delay_parm, int, 0);
......@@ -50,7 +50,7 @@ static u8 w1_crc8_table[] = {
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};
void w1_delay(unsigned long tm)
static void w1_delay(unsigned long tm)
{
udelay(tm * w1_delay_parm);
}
......@@ -61,7 +61,7 @@ static u8 w1_read_bit(struct w1_master *dev);
/**
* Generates a write-0 or write-1 cycle and samples the level.
*/
u8 w1_touch_bit(struct w1_master *dev, int bit)
static u8 w1_touch_bit(struct w1_master *dev, int bit)
{
if (dev->bus_master->touch_bit)
return dev->bus_master->touch_bit(dev->bus_master->data, bit);
......@@ -108,6 +108,7 @@ void w1_write_8(struct w1_master *dev, u8 byte)
for (i = 0; i < 8; ++i)
w1_touch_bit(dev, (byte >> i) & 0x1);
}
EXPORT_SYMBOL_GPL(w1_write_8);
/**
......@@ -176,7 +177,7 @@ u8 w1_triplet(struct w1_master *dev, int bdir)
* @param dev the master device
* @return the byte read
*/
u8 w1_read_8(struct w1_master * dev)
static u8 w1_read_8(struct w1_master * dev)
{
int i;
u8 res = 0;
......@@ -208,6 +209,7 @@ void w1_write_block(struct w1_master *dev, const u8 *buf, int len)
for (i = 0; i < len; ++i)
w1_write_8(dev, buf[i]);
}
EXPORT_SYMBOL_GPL(w1_write_block);
/**
* Reads a series of bytes.
......@@ -232,6 +234,7 @@ u8 w1_read_block(struct w1_master *dev, u8 *buf, int len)
return ret;
}
EXPORT_SYMBOL_GPL(w1_read_block);
/**
* Issues a reset bus sequence.
......@@ -257,6 +260,7 @@ int w1_reset_bus(struct w1_master *dev)
return result;
}
EXPORT_SYMBOL_GPL(w1_reset_bus);
u8 w1_calc_crc8(u8 * data, int len)
{
......@@ -267,14 +271,15 @@ u8 w1_calc_crc8(u8 * data, int len)
return crc;
}
EXPORT_SYMBOL_GPL(w1_calc_crc8);
void w1_search_devices(struct w1_master *dev, w1_slave_found_callback cb)
void w1_search_devices(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
{
dev->attempts++;
if (dev->bus_master->search)
dev->bus_master->search(dev->bus_master->data, cb);
dev->bus_master->search(dev->bus_master->data, search_type, cb);
else
w1_search(dev, cb);
w1_search(dev, search_type, cb);
}
/**
......@@ -299,14 +304,4 @@ int w1_reset_select_slave(struct w1_slave *sl)
}
return 0;
}
EXPORT_SYMBOL(w1_touch_bit);
EXPORT_SYMBOL(w1_write_8);
EXPORT_SYMBOL(w1_read_8);
EXPORT_SYMBOL(w1_reset_bus);
EXPORT_SYMBOL(w1_calc_crc8);
EXPORT_SYMBOL(w1_delay);
EXPORT_SYMBOL(w1_read_block);
EXPORT_SYMBOL(w1_write_block);
EXPORT_SYMBOL(w1_search_devices);
EXPORT_SYMBOL(w1_reset_select_slave);
EXPORT_SYMBOL_GPL(w1_reset_select_slave);
......@@ -24,11 +24,8 @@
#include "w1.h"
void w1_delay(unsigned long);
u8 w1_touch_bit(struct w1_master *, int);
u8 w1_triplet(struct w1_master *dev, int bdir);
void w1_write_8(struct w1_master *, u8);
u8 w1_read_8(struct w1_master *);
int w1_reset_bus(struct w1_master *);
u8 w1_calc_crc8(u8 *, int);
void w1_write_block(struct w1_master *, const u8 *, int);
......
......@@ -21,72 +21,225 @@
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/connector.h>
#include "w1.h"
#include "w1_log.h"
#include "w1_netlink.h"
#ifndef NETLINK_DISABLED
#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
{
unsigned int size;
struct sk_buff *skb;
struct w1_netlink_msg *data;
struct nlmsghdr *nlh;
char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)];
struct cn_msg *m = (struct cn_msg *)buf;
struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1);
if (!dev->nls)
return;
memset(buf, 0, sizeof(buf));
size = NLMSG_SPACE(sizeof(struct w1_netlink_msg));
m->id.idx = CN_W1_IDX;
m->id.val = CN_W1_VAL;
skb = alloc_skb(size, GFP_ATOMIC);
if (!skb) {
dev_err(&dev->dev, "skb_alloc() failed.\n");
return;
}
m->seq = dev->seq++;
m->len = sizeof(struct w1_netlink_msg);
memcpy(w, msg, sizeof(struct w1_netlink_msg));
cn_netlink_send(m, 0, GFP_KERNEL);
}
static int w1_process_command_master(struct w1_master *dev, struct cn_msg *msg,
struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
{
dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n",
__func__, dev->name, cmd->cmd, cmd->len);
if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH)
return -EINVAL;
w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
return 0;
}
static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg,
struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
{
void *data;
struct w1_netlink_msg *h;
struct w1_netlink_cmd *c;
struct cn_msg *cm;
int err;
data = kzalloc(sizeof(struct cn_msg) +
sizeof(struct w1_netlink_msg) +
sizeof(struct w1_netlink_cmd) +
cmd->len, GFP_KERNEL);
if (!data)
return -ENOMEM;
cm = (struct cn_msg *)(data);
h = (struct w1_netlink_msg *)(cm + 1);
c = (struct w1_netlink_cmd *)(h + 1);
memcpy(cm, msg, sizeof(struct cn_msg));
memcpy(h, hdr, sizeof(struct w1_netlink_msg));
memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
nlh = NLMSG_PUT(skb, 0, dev->seq++, NLMSG_DONE, size - sizeof(*nlh));
cm->ack = msg->seq+1;
cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
data = (struct w1_netlink_msg *)NLMSG_DATA(nlh);
h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
memcpy(data, msg, sizeof(struct w1_netlink_msg));
memcpy(c->data, cmd->data, c->len);
NETLINK_CB(skb).dst_group = dev->groups;
netlink_broadcast(dev->nls, skb, 0, dev->groups, GFP_ATOMIC);
err = cn_netlink_send(cm, 0, GFP_KERNEL);
nlmsg_failure:
return;
kfree(data);
return err;
}
int dev_init_netlink(struct w1_master *dev)
static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
{
dev->nls = netlink_kernel_create(NETLINK_W1, 1, NULL, THIS_MODULE);
if (!dev->nls) {
printk(KERN_ERR "Failed to create new netlink socket(%u) for w1 master %s.\n",
NETLINK_W1, dev->dev.bus_id);
int err = 0;
dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
__func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, sl->reg_num.crc,
cmd->cmd, cmd->len);
switch (cmd->cmd) {
case W1_CMD_READ:
w1_read_block(sl->master, cmd->data, cmd->len);
w1_send_read_reply(sl, msg, hdr, cmd);
break;
case W1_CMD_WRITE:
w1_write_block(sl->master, cmd->data, cmd->len);
break;
case W1_CMD_SEARCH:
case W1_CMD_ALARM_SEARCH:
w1_search_process(sl->master,
(cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
break;
default:
err = -1;
break;
}
return 0;
return err;
}
void dev_fini_netlink(struct w1_master *dev)
static void w1_cn_callback(void *data)
{
if (dev->nls && dev->nls->sk_socket)
sock_release(dev->nls->sk_socket);
struct cn_msg *msg = data;
struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
struct w1_netlink_cmd *cmd;
struct w1_slave *sl;
struct w1_master *dev;
int err = 0;
while (msg->len && !err) {
struct w1_reg_num id;
u16 mlen = m->len;
u8 *cmd_data = m->data;
dev = NULL;
sl = NULL;
memcpy(&id, m->id.id, sizeof(id));
#if 0
printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
__func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
#endif
if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
err = -E2BIG;
break;
}
if (!mlen)
goto out_cont;
if (m->type == W1_MASTER_CMD) {
dev = w1_search_master_id(m->id.mst.id);
} else if (m->type == W1_SLAVE_CMD) {
sl = w1_search_slave(&id);
if (sl)
dev = sl->master;
}
if (!dev) {
err = -ENODEV;
goto out_cont;
}
mutex_lock(&dev->mutex);
if (sl && w1_reset_select_slave(sl)) {
err = -ENODEV;
goto out_up;
}
while (mlen) {
cmd = (struct w1_netlink_cmd *)cmd_data;
if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
err = -E2BIG;
break;
}
if (sl)
w1_process_command_slave(sl, msg, m, cmd);
else
w1_process_command_master(dev, msg, m, cmd);
cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
}
out_up:
atomic_dec(&dev->refcnt);
if (sl)
atomic_dec(&sl->refcnt);
mutex_unlock(&dev->mutex);
out_cont:
msg->len -= sizeof(struct w1_netlink_msg) + m->len;
m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
/*
* Let's allow requests for nonexisting devices.
*/
if (err == -ENODEV)
err = 0;
}
#if 0
if (err) {
printk("%s: malformed message. Dropping.\n", __func__);
}
#endif
}
#else
#warning Netlink support is disabled. Please compile with NET support enabled.
int w1_init_netlink(void)
{
struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
}
void w1_fini_netlink(void)
{
struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
cn_del_callback(&w1_id);
}
#else
void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
{
}
int dev_init_netlink(struct w1_master *dev)
int w1_init_netlink(void)
{
return 0;
}
void dev_fini_netlink(struct w1_master *dev)
void w1_fini_netlink(void)
{
}
#endif
......@@ -23,6 +23,7 @@
#define __W1_NETLINK_H
#include <asm/types.h>
#include <linux/connector.h>
#include "w1.h"
......@@ -31,29 +32,43 @@ enum w1_netlink_message_types {
W1_SLAVE_REMOVE,
W1_MASTER_ADD,
W1_MASTER_REMOVE,
W1_MASTER_CMD,
W1_SLAVE_CMD,
};
struct w1_netlink_msg
{
__u8 type;
__u8 reserved[3];
union
{
struct w1_reg_num id;
__u64 w1_id;
struct
{
__u8 reserved;
__u16 len;
union {
__u8 id[8];
struct w1_mst {
__u32 id;
__u32 pid;
__u32 res;
} mst;
} id;
__u8 data[0];
};
#define W1_CMD_READ 0x0
#define W1_CMD_WRITE 0x1
#define W1_CMD_SEARCH 0x2
#define W1_CMD_ALARM_SEARCH 0x3
struct w1_netlink_cmd
{
__u8 cmd;
__u8 res;
__u16 len;
__u8 data[0];
};
#ifdef __KERNEL__
void w1_netlink_send(struct w1_master *, struct w1_netlink_msg *);
int dev_init_netlink(struct w1_master *dev);
void dev_fini_netlink(struct w1_master *dev);
int w1_init_netlink(void);
void w1_fini_netlink(void);
#endif /* __KERNEL__ */
#endif /* __W1_NETLINK_H */
......@@ -34,8 +34,11 @@
#define CN_VAL_PROC 0x1
#define CN_IDX_CIFS 0x2
#define CN_VAL_CIFS 0x1
#define CN_W1_IDX 0x3 /* w1 communication */
#define CN_W1_VAL 0x1
#define CN_NETLINK_USERS 1
#define CN_NETLINK_USERS 4
/*
* Maximum connector's message size.
......
......@@ -5,7 +5,7 @@
#include <linux/types.h>
#define NETLINK_ROUTE 0 /* Routing/device hook */
#define NETLINK_W1 1 /* 1-wire subsystem */
#define NETLINK_UNUSED 1 /* Unused number */
#define NETLINK_USERSOCK 2 /* Reserved for user mode socket protocols */
#define NETLINK_FIREWALL 3 /* Firewalling hook */
#define NETLINK_INET_DIAG 4 /* INET socket monitoring */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment