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"
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 DS9490R UWB <-> W1 bridge.
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 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.
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)))
......
......@@ -24,7 +24,136 @@
#include <linux/mod_devicetable.h>
#include <linux/usb.h>
#include "dscore.h"
#include "../w1_int.h"
#include "../w1.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 list_head ds_entry;
struct usb_device *udev;
struct usb_interface *intf;
int ep[NUM_EP];
struct w1_bus_master master;
};
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;
};
static struct usb_device_id ds_id_table [] = {
{ USB_DEVICE(0x04fa, 0x2490) },
......@@ -35,21 +164,12 @@ MODULE_DEVICE_TABLE(usb, ds_id_table);
static int ds_probe(struct usb_interface *, const struct usb_device_id *);
static void ds_disconnect(struct usb_interface *);
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);
static int ds_start_pulse(struct ds_device *, int);
int ds_reset(struct ds_device *, struct ds_status *);
struct ds_device * ds_get_device(void);
void ds_put_device(struct ds_device *);
static inline void ds_dump_status(unsigned char *, unsigned char *, int);
static int ds_send_control(struct ds_device *, u16, u16);
static int ds_send_control_mode(struct ds_device *, u16, u16);
static int ds_send_control_cmd(struct ds_device *, u16, u16);
static LIST_HEAD(ds_devices);
static DEFINE_MUTEX(ds_mutex);
static struct usb_driver ds_driver = {
.name = "DS9490R",
......@@ -58,20 +178,6 @@ static struct usb_driver ds_driver = {
.id_table = ds_id_table,
};
static struct ds_device *ds_dev;
struct ds_device * ds_get_device(void)
{
if (ds_dev)
atomic_inc(&ds_dev->refcnt);
return ds_dev;
}
void ds_put_device(struct ds_device *dev)
{
atomic_dec(&dev->refcnt);
}
static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
{
int err;
......@@ -86,7 +192,7 @@ static int ds_send_control_cmd(struct ds_device *dev, u16 value, u16 index)
return err;
}
#if 0
static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
{
int err;
......@@ -101,7 +207,7 @@ static int ds_send_control_mode(struct ds_device *dev, u16 value, u16 index)
return err;
}
#endif
static int ds_send_control(struct ds_device *dev, u16 value, u16 index)
{
int err;
......@@ -324,7 +430,7 @@ static int ds_wait_status(struct ds_device *dev, struct ds_status *st)
return 0;
}
int ds_reset(struct ds_device *dev, struct ds_status *st)
static int ds_reset(struct ds_device *dev, struct ds_status *st)
{
int err;
......@@ -345,7 +451,7 @@ int ds_reset(struct ds_device *dev, struct ds_status *st)
}
#if 0
int ds_set_speed(struct ds_device *dev, int speed)
static int ds_set_speed(struct ds_device *dev, int speed)
{
int err;
......@@ -395,7 +501,7 @@ static int ds_start_pulse(struct ds_device *dev, int delay)
return err;
}
int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
static int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
{
int err, count;
struct ds_status st;
......@@ -427,7 +533,7 @@ int ds_touch_bit(struct ds_device *dev, u8 bit, u8 *tbit)
return 0;
}
int ds_write_bit(struct ds_device *dev, u8 bit)
static int ds_write_bit(struct ds_device *dev, u8 bit)
{
int err;
struct ds_status st;
......@@ -441,7 +547,7 @@ int ds_write_bit(struct ds_device *dev, u8 bit)
return 0;
}
int ds_write_byte(struct ds_device *dev, u8 byte)
static int ds_write_byte(struct ds_device *dev, u8 byte)
{
int err;
struct ds_status st;
......@@ -464,26 +570,7 @@ int ds_write_byte(struct ds_device *dev, u8 byte)
return !(byte == rbyte);
}
int ds_read_bit(struct ds_device *dev, u8 *bit)
{
int err;
err = ds_send_control_mode(dev, MOD_PULSE_EN, PULSE_SPUE);
if (err)
return err;
err = ds_send_control(dev, COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D, 0);
if (err)
return err;
err = ds_recv_data(dev, bit, sizeof(*bit));
if (err < 0)
return err;
return 0;
}
int ds_read_byte(struct ds_device *dev, u8 *byte)
static int ds_read_byte(struct ds_device *dev, u8 *byte)
{
int err;
struct ds_status st;
......@@ -501,7 +588,7 @@ int ds_read_byte(struct ds_device *dev, u8 *byte)
return 0;
}
int ds_read_block(struct ds_device *dev, u8 *buf, int len)
static int ds_read_block(struct ds_device *dev, u8 *buf, int len)
{
struct ds_status st;
int err;
......@@ -527,7 +614,7 @@ int ds_read_block(struct ds_device *dev, u8 *buf, int len)
return err;
}
int ds_write_block(struct ds_device *dev, u8 *buf, int len)
static int ds_write_block(struct ds_device *dev, u8 *buf, int len)
{
int err;
struct ds_status st;
......@@ -555,7 +642,7 @@ int ds_write_block(struct ds_device *dev, u8 *buf, int len)
#if 0
int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
static int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int conditional_search)
{
int err;
u16 value, index;
......@@ -584,7 +671,7 @@ int ds_search(struct ds_device *dev, u64 init, u64 *buf, u8 id_number, int condi
return err/8;
}
int ds_match_access(struct ds_device *dev, u64 init)
static int ds_match_access(struct ds_device *dev, u64 init)
{
int err;
struct ds_status st;
......@@ -604,7 +691,7 @@ int ds_match_access(struct ds_device *dev, u64 init)
return 0;
}
int ds_set_path(struct ds_device *dev, u64 init)
static int ds_set_path(struct ds_device *dev, u64 init)
{
int err;
struct ds_status st;
......@@ -630,45 +717,156 @@ int ds_set_path(struct ds_device *dev, u64 init)
#endif /* 0 */
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;
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 ds_w1_init(struct ds_device *dev)
{
memset(&dev->master, 0, sizeof(struct w1_bus_master));
dev->master.data = dev;
dev->master.touch_bit = &ds9490r_touch_bit;
dev->master.read_bit = &ds9490r_read_bit;
dev->master.write_bit = &ds9490r_write_bit;
dev->master.read_byte = &ds9490r_read_byte;
dev->master.write_byte = &ds9490r_write_byte;
dev->master.read_block = &ds9490r_read_block;
dev->master.write_block = &ds9490r_write_block;
dev->master.reset_bus = &ds9490r_reset;
return w1_add_master_device(&dev->master);
}
static void ds_w1_fini(struct ds_device *dev)
{
w1_remove_master_device(&dev->master);
}
static int ds_probe(struct usb_interface *intf,
const struct usb_device_id *udev_id)
{
struct usb_device *udev = interface_to_usbdev(intf);
struct usb_endpoint_descriptor *endpoint;
struct usb_host_interface *iface_desc;
struct ds_device *dev;
int i, err;
ds_dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
if (!ds_dev) {
dev = kmalloc(sizeof(struct ds_device), GFP_KERNEL);
if (!dev) {
printk(KERN_INFO "Failed to allocate new DS9490R structure.\n");
return -ENOMEM;
}
dev->udev = usb_get_dev(udev);
if (!dev->udev) {
err = -ENOMEM;
goto err_out_free;
}
memset(dev->ep, 0, sizeof(dev->ep));
ds_dev->udev = usb_get_dev(udev);
usb_set_intfdata(intf, ds_dev);
usb_set_intfdata(intf, dev);
err = usb_set_interface(ds_dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
err = usb_set_interface(dev->udev, intf->altsetting[0].desc.bInterfaceNumber, 3);
if (err) {
printk(KERN_ERR "Failed to set alternative setting 3 for %d interface: err=%d.\n",
intf->altsetting[0].desc.bInterfaceNumber, err);
return err;
goto err_out_clear;
}
err = usb_reset_configuration(ds_dev->udev);
err = usb_reset_configuration(dev->udev);
if (err) {
printk(KERN_ERR "Failed to reset configuration: err=%d.\n", err);
return err;
goto err_out_clear;
}
iface_desc = &intf->altsetting[0];
if (iface_desc->desc.bNumEndpoints != NUM_EP-1) {
printk(KERN_INFO "Num endpoints=%d. It is not DS9490R.\n", iface_desc->desc.bNumEndpoints);
return -ENODEV;
err = -EINVAL;
goto err_out_clear;
}
atomic_set(&ds_dev->refcnt, 0);
memset(ds_dev->ep, 0, sizeof(ds_dev->ep));
/*
* This loop doesn'd show control 0 endpoint,
* so we will fill only 1-3 endpoints entry.
......@@ -676,54 +874,31 @@ static int ds_probe(struct usb_interface *intf,
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
ds_dev->ep[i+1] = endpoint->bEndpointAddress;
dev->ep[i+1] = endpoint->bEndpointAddress;
#if 0
printk("%d: addr=%x, size=%d, dir=%s, type=%x\n",
i, endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize),
(endpoint->bEndpointAddress & USB_DIR_IN)?"IN":"OUT",
endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
#endif
}
#if 0
{
int err, i;
u64 buf[3];
u64 init=0xb30000002078ee81ull;
struct ds_status st;
ds_reset(ds_dev, &st);
err = ds_search(ds_dev, init, buf, 3, 0);
if (err < 0)
return err;
for (i=0; i<err; ++i)
printk("%d: %llx\n", i, buf[i]);
printk("Resetting...\n");
ds_reset(ds_dev, &st);
printk("Setting path for %llx.\n", init);
err = ds_set_path(ds_dev, init);
err = ds_w1_init(dev);
if (err)
return err;
printk("Calling MATCH_ACCESS.\n");
err = ds_match_access(ds_dev, init);
if (err)
return err;
printk("Searching the bus...\n");
err = ds_search(ds_dev, init, buf, 3, 0);
goto err_out_clear;
printk("ds_search() returned %d\n", err);
if (err < 0)
return err;
for (i=0; i<err; ++i)
printk("%d: %llx\n", i, buf[i]);
mutex_lock(&ds_mutex);
list_add_tail(&dev->ds_entry, &ds_devices);
mutex_unlock(&ds_mutex);
return 0;
}
#endif
return 0;
err_out_clear:
usb_set_intfdata(intf, NULL);
usb_put_dev(dev->udev);
err_out_free:
kfree(dev);
return err;
}
static void ds_disconnect(struct usb_interface *intf)
......@@ -731,19 +906,19 @@ static void ds_disconnect(struct usb_interface *intf)
struct ds_device *dev;
dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
if (!dev)
return;
while (atomic_read(&dev->refcnt)) {
printk(KERN_INFO "Waiting for DS to become free: refcnt=%d.\n",
atomic_read(&dev->refcnt));
mutex_lock(&ds_mutex);
list_del(&dev->ds_entry);
mutex_unlock(&ds_mutex);
if (msleep_interruptible(1000))
flush_signals(current);
}
ds_w1_fini(dev);
usb_set_intfdata(intf, NULL);
usb_put_dev(dev->udev);
kfree(dev);
ds_dev = NULL;
}
static int ds_init(void)
......@@ -769,27 +944,4 @@ module_exit(ds_fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
EXPORT_SYMBOL(ds_touch_bit);
EXPORT_SYMBOL(ds_read_byte);
EXPORT_SYMBOL(ds_read_bit);
EXPORT_SYMBOL(ds_read_block);
EXPORT_SYMBOL(ds_write_byte);
EXPORT_SYMBOL(ds_write_bit);
EXPORT_SYMBOL(ds_write_block);
EXPORT_SYMBOL(ds_reset);
EXPORT_SYMBOL(ds_get_device);
EXPORT_SYMBOL(ds_put_device);
/*
* This functions can be used for EEPROM programming,
* when driver will be included into mainline this will
* require uncommenting.
*/
#if 0
EXPORT_SYMBOL(ds_start_pulse);
EXPORT_SYMBOL(ds_set_speed);
EXPORT_SYMBOL(ds_detect);
EXPORT_SYMBOL(ds_stop_pulse);
EXPORT_SYMBOL(ds_search);
#endif
MODULE_DESCRIPTION("DS2490 USB <-> W1 bus master driver (DS9490*)");
/*
* 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;
}
......
......@@ -35,7 +35,6 @@
#include <asm/atomic.h>
#include "w1.h"
#include "w1_io.h"
#include "w1_log.h"
#include "w1_int.h"
#include "w1_family.h"
......@@ -55,7 +54,7 @@ module_param_named(control_timeout, w1_control_timeout, int, 0);
module_param_named(max_slave_count, w1_max_slave_count, int, 0);
module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
DEFINE_SPINLOCK(w1_mlock);
DEFINE_MUTEX(w1_mlock);
LIST_HEAD(w1_masters);
static struct task_struct *w1_control_thread;
......@@ -75,8 +74,6 @@ static void w1_master_release(struct device *dev)
struct w1_master *md = dev_to_w1_master(dev);
dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
dev_fini_netlink(md);
memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
kfree(md);
}
......@@ -85,10 +82,10 @@ static void w1_slave_release(struct device *dev)
{
struct w1_slave *sl = dev_to_w1_slave(dev);
dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name);
printk("%s: Releasing %s.\n", __func__, sl->name);
while (atomic_read(&sl->refcnt)) {
dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n",
printk("Waiting for %s to become free: refcnt=%d.\n",
sl->name, atomic_read(&sl->refcnt));
if (msleep_interruptible(1000))
flush_signals(current);
......@@ -111,7 +108,6 @@ static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, siz
{
struct w1_slave *sl = kobj_to_w1_slave(kobj);
atomic_inc(&sl->refcnt);
if (off > 8) {
count = 0;
} else {
......@@ -120,7 +116,6 @@ static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, siz
memcpy(buf, (u8 *)&sl->reg_num, count);
}
atomic_dec(&sl->refcnt);
return count;
}
......@@ -139,7 +134,63 @@ static struct bin_attribute w1_slave_attr_bin_id = {
};
/* Default family */
static struct w1_family w1_default_family;
static ssize_t w1_default_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
{
struct w1_slave *sl = kobj_to_w1_slave(kobj);
mutex_lock(&sl->master->mutex);
if (w1_reset_select_slave(sl)) {
count = 0;
goto out_up;
}
w1_write_block(sl->master, buf, count);
out_up:
mutex_unlock(&sl->master->mutex);
return count;
}
static ssize_t w1_default_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
{
struct w1_slave *sl = kobj_to_w1_slave(kobj);
mutex_lock(&sl->master->mutex);
w1_read_block(sl->master, buf, count);
mutex_unlock(&sl->master->mutex);
return count;
}
static struct bin_attribute w1_default_attr = {
.attr = {
.name = "rw",
.mode = S_IRUGO | S_IWUSR,
.owner = THIS_MODULE,
},
.size = PAGE_SIZE,
.read = w1_default_read,
.write = w1_default_write,
};
static int w1_default_add_slave(struct w1_slave *sl)
{
return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
}
static void w1_default_remove_slave(struct w1_slave *sl)
{
sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
}
static struct w1_family_ops w1_default_fops = {
.add_slave = w1_default_add_slave,
.remove_slave = w1_default_remove_slave,
};
static struct w1_family w1_default_family = {
.fops = &w1_default_fops,
};
static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
......@@ -183,12 +234,9 @@ static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_a
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible (&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "%s\n", md->name);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -199,12 +247,9 @@ static ssize_t w1_master_attribute_store_search(struct device * dev,
{
struct w1_master *md = dev_to_w1_master(dev);
if (down_interruptible (&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
md->search_count = simple_strtol(buf, NULL, 0);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -216,12 +261,9 @@ static ssize_t w1_master_attribute_show_search(struct device *dev,
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible (&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "%d\n", md->search_count);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -231,12 +273,9 @@ static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct devic
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible(&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "0x%p\n", md->bus_master);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -252,12 +291,9 @@ static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, stru
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible(&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "%d\n", md->max_slave_count);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -266,12 +302,9 @@ static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct devi
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible(&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "%lu\n", md->attempts);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -280,12 +313,9 @@ static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct d
struct w1_master *md = dev_to_w1_master(dev);
ssize_t count;
if (down_interruptible(&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
count = sprintf(buf, "%d\n", md->slave_count);
up(&md->mutex);
mutex_unlock(&md->mutex);
return count;
}
......@@ -294,8 +324,7 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device
struct w1_master *md = dev_to_w1_master(dev);
int c = PAGE_SIZE;
if (down_interruptible(&md->mutex))
return -EBUSY;
mutex_lock(&md->mutex);
if (md->slave_count == 0)
c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
......@@ -310,7 +339,7 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device
}
}
up(&md->mutex);
mutex_unlock(&md->mutex);
return PAGE_SIZE - c;
}
......@@ -362,7 +391,8 @@ static void w1_destroy_master_attributes(struct w1_master *master)
}
#ifdef CONFIG_HOTPLUG
static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
static int w1_uevent(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
struct w1_master *md = NULL;
struct w1_slave *sl = NULL;
......@@ -382,7 +412,8 @@ static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer
return -EINVAL;
}
dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id);
dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
event_owner, name, dev->bus_id);
if (dev->driver != &w1_slave_driver || !sl)
return 0;
......@@ -401,7 +432,8 @@ static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer
return 0;
};
#else
static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
static int w1_uevent(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
return 0;
}
......@@ -425,7 +457,8 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
(unsigned int) sl->reg_num.family,
(unsigned long long) sl->reg_num.id);
dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
&sl->dev.bus_id[0]);
err = device_register(&sl->dev);
if (err < 0) {
......@@ -496,6 +529,7 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
sl->master = dev;
set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
memset(&msg, 0, sizeof(msg));
memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
atomic_set(&sl->refcnt, 0);
init_completion(&sl->released);
......@@ -526,7 +560,7 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
sl->ttl = dev->slave_ttl;
dev->slave_count++;
memcpy(&msg.id.id, rn, sizeof(msg.id.id));
memcpy(msg.id.id, rn, sizeof(msg.id));
msg.type = W1_SLAVE_ADD;
w1_netlink_send(dev, &msg);
......@@ -544,7 +578,8 @@ static void w1_slave_detach(struct w1_slave *sl)
if (sl->family->fops && sl->family->fops->remove_slave)
sl->family->fops->remove_slave(sl);
memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
memset(&msg, 0, sizeof(msg));
memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
msg.type = W1_SLAVE_REMOVE;
w1_netlink_send(sl->master, &msg);
......@@ -561,7 +596,7 @@ static struct w1_master *w1_search_master(void *data)
struct w1_master *dev;
int found = 0;
spin_lock_bh(&w1_mlock);
mutex_lock(&w1_mlock);
list_for_each_entry(dev, &w1_masters, w1_master_entry) {
if (dev->bus_master->data == data) {
found = 1;
......@@ -569,22 +604,69 @@ static struct w1_master *w1_search_master(void *data)
break;
}
}
spin_unlock_bh(&w1_mlock);
mutex_unlock(&w1_mlock);
return (found)?dev:NULL;
}
struct w1_master *w1_search_master_id(u32 id)
{
struct w1_master *dev;
int found = 0;
mutex_lock(&w1_mlock);
list_for_each_entry(dev, &w1_masters, w1_master_entry) {
if (dev->id == id) {
found = 1;
atomic_inc(&dev->refcnt);
break;
}
}
mutex_unlock(&w1_mlock);
return (found)?dev:NULL;
}
struct w1_slave *w1_search_slave(struct w1_reg_num *id)
{
struct w1_master *dev;
struct w1_slave *sl = NULL;
int found = 0;
mutex_lock(&w1_mlock);
list_for_each_entry(dev, &w1_masters, w1_master_entry) {
mutex_lock(&dev->mutex);
list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
if (sl->reg_num.family == id->family &&
sl->reg_num.id == id->id &&
sl->reg_num.crc == id->crc) {
found = 1;
atomic_inc(&dev->refcnt);
atomic_inc(&sl->refcnt);
break;
}
}
mutex_unlock(&dev->mutex);
if (found)
break;
}
mutex_unlock(&w1_mlock);
return (found)?sl:NULL;
}
void w1_reconnect_slaves(struct w1_family *f)
{
struct w1_master *dev;
spin_lock_bh(&w1_mlock);
mutex_lock(&w1_mlock);
list_for_each_entry(dev, &w1_masters, w1_master_entry) {
dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
dev->name, f->fid);
set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
}
spin_unlock_bh(&w1_mlock);
mutex_unlock(&w1_mlock);
}
static void w1_slave_found(void *data, u64 rn)
......@@ -646,7 +728,7 @@ static void w1_slave_found(void *data, u64 rn)
* @dev The master device to search
* @cb Function to call when a device is found
*/
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)
{
u64 last_rn, rn, tmp64;
int i, slave_count = 0;
......@@ -677,7 +759,7 @@ void w1_search(struct w1_master *dev, w1_slave_found_callback cb)
}
/* Start the search */
w1_write_8(dev, W1_SEARCH);
w1_write_8(dev, search_type);
for (i = 0; i < 64; ++i) {
/* Determine the direction/search bit */
if (i == desc_bit)
......@@ -739,23 +821,23 @@ static int w1_control(void *data)
if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
spin_lock(&w1_mlock);
mutex_lock(&w1_mlock);
list_del(&dev->w1_master_entry);
spin_unlock(&w1_mlock);
mutex_unlock(&w1_mlock);
down(&dev->mutex);
mutex_lock(&dev->mutex);
list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
w1_slave_detach(sl);
}
w1_destroy_master_attributes(dev);
up(&dev->mutex);
mutex_unlock(&dev->mutex);
atomic_dec(&dev->refcnt);
continue;
}
if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
down(&dev->mutex);
mutex_lock(&dev->mutex);
list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
if (sl->family->fid == W1_FAMILY_DEFAULT) {
struct w1_reg_num rn;
......@@ -768,7 +850,7 @@ static int w1_control(void *data)
}
dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
up(&dev->mutex);
mutex_unlock(&dev->mutex);
}
}
}
......@@ -776,31 +858,14 @@ static int w1_control(void *data)
return 0;
}
int w1_process(void *data)
void w1_search_process(struct w1_master *dev, u8 search_type)
{
struct w1_master *dev = (struct w1_master *) data;
struct w1_slave *sl, *sln;
while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
try_to_freeze();
msleep_interruptible(w1_timeout * 1000);
if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
break;
if (!dev->initialized)
continue;
if (dev->search_count == 0)
continue;
if (down_interruptible(&dev->mutex))
continue;
list_for_each_entry(sl, &dev->slist, w1_slave_entry)
clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
w1_search_devices(dev, w1_slave_found);
w1_search_devices(dev, search_type, w1_slave_found);
list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
......@@ -813,8 +878,28 @@ int w1_process(void *data)
if (dev->search_count > 0)
dev->search_count--;
}
up(&dev->mutex);
int w1_process(void *data)
{
struct w1_master *dev = (struct w1_master *) data;
while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
try_to_freeze();
msleep_interruptible(w1_timeout * 1000);
if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
break;
if (!dev->initialized)
continue;
if (dev->search_count == 0)
continue;
mutex_lock(&dev->mutex);
w1_search_process(dev, W1_SEARCH);
mutex_unlock(&dev->mutex);
}
atomic_dec(&dev->refcnt);
......@@ -828,6 +913,8 @@ static int w1_init(void)
printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
w1_init_netlink();
retval = bus_register(&w1_bus_type);
if (retval) {
printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
......@@ -880,6 +967,8 @@ static void w1_fini(void)
list_for_each_entry(dev, &w1_masters, w1_master_entry)
__w1_remove_master_device(dev);
w1_fini_netlink();
kthread_stop(w1_control_thread);
driver_unregister(&w1_slave_driver);
......
......@@ -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);
nlh = NLMSG_PUT(skb, 0, dev->seq++, NLMSG_DONE, size - sizeof(*nlh));
memcpy(w, msg, sizeof(struct w1_netlink_msg));
data = (struct w1_netlink_msg *)NLMSG_DATA(nlh);
cn_netlink_send(m, 0, GFP_KERNEL);
}
memcpy(data, msg, sizeof(struct w1_netlink_msg));
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);
NETLINK_CB(skb).dst_group = dev->groups;
netlink_broadcast(dev->nls, skb, 0, dev->groups, GFP_ATOMIC);
if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH)
return -EINVAL;
nlmsg_failure:
return;
w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
return 0;
}
int dev_init_netlink(struct w1_master *dev)
static int w1_send_read_reply(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);
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));
cm->ack = msg->seq+1;
cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
memcpy(c->data, cmd->data, c->len);
err = cn_netlink_send(cm, 0, GFP_KERNEL);
kfree(data);
return err;
}
static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
{
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;
}
static void w1_cn_callback(void *data)
{
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
}
void dev_fini_netlink(struct w1_master *dev)
int w1_init_netlink(void)
{
if (dev->nls && dev->nls->sk_socket)
sock_release(dev->nls->sk_socket);
struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
}
#else
#warning Netlink support is disabled. Please compile with NET support enabled.
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