| @@ -33,7 +33,8 @@ struct i2c_smbus_alert { · 32 unmodified lines |
| 33 | 33 | |
| 34 | 34 | struct alert_data { |
| 35 | 35 | unsigned short addr; |
| 36 | | − u8 flag:1; |
| 36 | + enum i2c_alert_protocol type; |
| 37 | + unsigned int data; |
| 37 | 38 | }; |
| 38 | 39 | |
| 39 | 40 | /* If this is the alerting device, notify its driver */ |
| @@ -56,8 +57,7 @@ static int smbus_do_alert(struct device *dev, void *addrp) · 16 unmodified lines |
| 56 | 57 | if (client->dev.driver) { |
| 57 | 58 | driver = to_i2c_driver(client->dev.driver); |
| 58 | 59 | if (driver->alert) |
| 59 | | − driver->alert(client, I2C_PROTOCOL_SMBUS_ALERT, |
| 60 | | − data->flag); |
| 60 | + driver->alert(client, data->type, data->data); |
| 61 | 61 | else |
| 62 | 62 | dev_warn(&client->dev, "no driver alert()!\n"); |
| 63 | 63 | } else |
| @@ -97,8 +97,9 @@ static void smbus_alert(struct work_struct *work) · 33 unmodified lines |
| 97 | 97 | if (status < 0) |
| 98 | 98 | break; |
| 99 | 99 | |
| 100 | | − data.flag = status & 1; |
| 100 | + data.data = status & 1; |
| 101 | 101 | data.addr = status >> 1; |
| 102 | + data.type = I2C_PROTOCOL_SMBUS_ALERT; |
| 102 | 103 | |
| 103 | 104 | if (data.addr == prev_addr) { |
| 104 | 105 | dev_warn(&ara->dev, "Duplicate SMBALERT# from dev " |
| @@ -106,7 +107,7 @@ static void smbus_alert(struct work_struct *work) · 1 unmodified line |
| 106 | 107 | break; |
| 107 | 108 | } |
| 108 | 109 | dev_dbg(&ara->dev, "SMBALERT# from dev 0x%02x, flag %d\n", |
| 109 | | − data.addr, data.flag); |
| 110 | + data.addr, data.data); |
| 110 | 111 | |
| 111 | 112 | /* Notify driver for the device which issued the alert */ |
| 112 | 113 | device_for_each_child(&ara->adapter->dev, &data, |
| @@ -240,6 +241,108 @@ int i2c_handle_smbus_alert(struct i2c_client *ara) · 127 unmodified lines |
| 240 | 241 | } |
| 241 | 242 | EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert); |
| 242 | 243 | |
| 244 | +static void smbus_host_notify_work(struct work_struct *work) |
| 245 | +{ |
| 246 | + struct alert_data alert; |
| 247 | + struct i2c_adapter *adapter; |
| 248 | + unsigned long flags; |
| 249 | + u16 payload; |
| 250 | + u8 addr; |
| 251 | + struct smbus_host_notify *data; |
| 252 | + |
| 253 | + data = container_of(work, struct smbus_host_notify, work); |
| 254 | + |
| 255 | + spin_lock_irqsave(&data->lock, flags); |
| 256 | + payload = data->payload; |
| 257 | + addr = data->addr; |
| 258 | + adapter = data->adapter; |
| 259 | + |
| 260 | + /* clear the pending bit and release the spinlock */ |
| 261 | + data->pending = false; |
| 262 | + spin_unlock_irqrestore(&data->lock, flags); |
| 263 | + |
| 264 | + if (!adapter || !addr) |
| 265 | + return; |
| 266 | + |
| 267 | + alert.type = I2C_PROTOCOL_SMBUS_HOST_NOTIFY; |
| 268 | + alert.addr = addr; |
| 269 | + alert.data = payload; |
| 270 | + |
| 271 | + device_for_each_child(&adapter->dev, &alert, smbus_do_alert); |
| 272 | +} |
| 273 | + |
| 274 | +/** |
| 275 | + * i2c_setup_smbus_host_notify - Allocate a new smbus_host_notify for the given |
| 276 | + * I2C adapter. |
| 277 | + * @adapter: the adapter we want to associate a Host Notify function |
| 278 | + * |
| 279 | + * Returns a struct smbus_host_notify pointer on success, and NULL on failure. |
| 280 | + * The resulting smbus_host_notify must not be freed afterwards, it is a |
| 281 | + * managed resource already. |
| 282 | + */ |
| 283 | +struct smbus_host_notify *i2c_setup_smbus_host_notify(struct i2c_adapter *adap) |
| 284 | +{ |
| 285 | + struct smbus_host_notify *host_notify; |
| 286 | + |
| 287 | + host_notify = devm_kzalloc(&adap->dev, sizeof(struct smbus_host_notify), |
| 288 | + GFP_KERNEL); |
| 289 | + if (!host_notify) |
| 290 | + return NULL; |
| 291 | + |
| 292 | + host_notify->adapter = adap; |
| 293 | + |
| 294 | + spin_lock_init(&host_notify->lock); |
| 295 | + INIT_WORK(&host_notify->work, smbus_host_notify_work); |
| 296 | + |
| 297 | + return host_notify; |
| 298 | +} |
| 299 | +EXPORT_SYMBOL_GPL(i2c_setup_smbus_host_notify); |
| 300 | + |
| 301 | +/** |
| 302 | + * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct |
| 303 | + * I2C client. |
| 304 | + * @host_notify: the struct host_notify attached to the relevant adapter |
| 305 | + * @data: the Host Notify data which contains the payload and address of the |
| 306 | + * client |
| 307 | + * Context: can't sleep |
| 308 | + * |
| 309 | + * Helper function to be called from an I2C bus driver's interrupt |
| 310 | + * handler. It will schedule the Host Notify work, in turn calling the |
| 311 | + * corresponding I2C device driver's alert function. |
| 312 | + * |
| 313 | + * host_notify should be a valid pointer previously returned by |
| 314 | + * i2c_setup_smbus_host_notify(). |
| 315 | + */ |
| 316 | +int i2c_handle_smbus_host_notify(struct smbus_host_notify *host_notify, |
| 317 | + unsigned short addr, unsigned int data) |
| 318 | +{ |
| 319 | + unsigned long flags; |
| 320 | + struct i2c_adapter *adapter; |
| 321 | + |
| 322 | + if (!host_notify || !host_notify->adapter) |
| 323 | + return -EINVAL; |
| 324 | + |
| 325 | + adapter = host_notify->adapter; |
| 326 | + |
| 327 | + spin_lock_irqsave(&host_notify->lock, flags); |
| 328 | + |
| 329 | + if (host_notify->pending) { |
| 330 | + spin_unlock_irqrestore(&host_notify->lock, flags); |
| 331 | + dev_warn(&adapter->dev, "Host Notify already scheduled.\n"); |
| 332 | + return -EBUSY; |
| 333 | + } |
| 334 | + |
| 335 | + host_notify->payload = data; |
| 336 | + host_notify->addr = addr; |
| 337 | + |
| 338 | + /* Mark that there is a pending notification and release the lock */ |
| 339 | + host_notify->pending = true; |
| 340 | + spin_unlock_irqrestore(&host_notify->lock, flags); |
| 341 | + |
| 342 | + return schedule_work(&host_notify->work); |
| 343 | +} |
| 344 | +EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); |
| 345 | + |
| 243 | 346 | module_i2c_driver(smbalert_driver); |
| 244 | 347 | |
| 245 | 348 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |