2007-08-23 22:46:41

by Greg KH

[permalink] [raw]
Subject: [RFC] USB: driver for iphone charging


my berry_charge code that adds support for charging the iphone when it
is plugged into a Linux machine.

As I don't have an iphone, can someone who does please test this out and
let me know if it works properly or not?

Matt, I fixed up the formatting of your original driver, fixed a sparse
warning, and the memory leak (which happens to also be in the
berry_charge driver, I'll go fix that too...)

Cc: Matt Colyer <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>

---
drivers/usb/Makefile | 1
drivers/usb/misc/Kconfig | 11 ++++
drivers/usb/misc/Makefile | 1
drivers/usb/misc/iphone.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 120 insertions(+)

--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_USB_FTDI_ELAN) += misc/
obj-$(CONFIG_USB_GOTEMP) += misc/
obj-$(CONFIG_USB_IDMOUSE) += misc/
obj-$(CONFIG_USB_IOWARRIOR) += misc/
+obj-$(CONFIG_USB_IPHONE) += misc/
obj-$(CONFIG_USB_LCD) += misc/
obj-$(CONFIG_USB_LD) += misc/
obj-$(CONFIG_USB_LED) += misc/
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -99,6 +99,17 @@ config USB_BERRY_CHARGE
To compile this driver as a module, choose M here: the
module will be called berry_charge.

+config USB_IPHONE
+ tristate "USB iPhone recharge support"
+ depends on USB
+ help
+ Say Y here if you want to connect a iPhone device to your
+ computer's USB port and have it automatically switch to "recharge"
+ mode.
+
+ To compile this driver as a module, choose M here: the
+ module will be called iphone.
+
config USB_LED
tristate "USB LED driver support"
depends on USB
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_USB_FTDI_ELAN) += ftdi-elan
obj-$(CONFIG_USB_GOTEMP) += gotemp.o
obj-$(CONFIG_USB_IDMOUSE) += idmouse.o
obj-$(CONFIG_USB_IOWARRIOR) += iowarrior.o
+obj-$(CONFIG_USB_IPHONE) += iphone.o
obj-$(CONFIG_USB_LCD) += usblcd.o
obj-$(CONFIG_USB_LD) += ldusb.o
obj-$(CONFIG_USB_LED) += usbled.o
--- /dev/null
+++ b/drivers/usb/misc/iphone.c
@@ -0,0 +1,107 @@
+/*
+ * USB iPhone module
+ *
+ * Copyright (C) 2007 Greg Kroah-Hartman <[email protected]>
+ * Copyright (C) 2007 Matt Colyer <[email protected]>
+ *
+ * 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, version 2.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+
+#define APPLE_VENDOR 0x05ac
+#define IPHONE 0x1290
+
+static int debug;
+
+#ifdef dbg
+#undef dbg
+#endif
+#define dbg(dev, format, arg...) \
+ if (debug) \
+ dev_printk(KERN_DEBUG , dev , format , ## arg)
+
+static struct usb_device_id id_table [] = {
+ { USB_DEVICE(APPLE_VENDOR, IPHONE) },
+ { }, /* Terminating entry */
+};
+MODULE_DEVICE_TABLE(usb, id_table);
+
+static int select_configuration(struct usb_device *udev)
+{
+ char *dummy_buffer = kzalloc(2, GFP_KERNEL);
+ int retval;
+
+ if (!dummy_buffer)
+ return -ENOMEM;
+
+ dbg(&udev->dev, "Calling set_configuration\n");
+ retval = usb_driver_set_configuration(udev, 3);
+ if (retval) {
+ dev_err(&udev->dev, "Set Configuration failed :%d.\n", retval);
+ goto exit;
+ }
+
+ dbg(&udev->dev, "Sending first magic command\n");
+ retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x40, 0x40,
+ 0x6400, 0, dummy_buffer, 0, 100);
+
+exit:
+ kfree(dummy_buffer);
+ return retval;
+}
+
+static int iphone_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ struct usb_device *udev = interface_to_usbdev(intf);
+
+ if (udev->actconfig->desc.bConfigurationValue == 3) {
+ dbg(&udev->dev, "Configuration changed");
+ return -ENODEV;
+ }
+
+ /* turn the power on */
+ select_configuration(udev);
+
+ /* we don't really want to bind to the device, userspace programs can
+ * handle the syncing just fine, so get outta here. */
+ return -ENODEV;
+}
+
+static void iphone_disconnect(struct usb_interface *intf)
+{
+}
+
+static struct usb_driver iphone_driver = {
+ .name = "iphone",
+ .probe = iphone_probe,
+ .disconnect = iphone_disconnect,
+ .id_table = id_table,
+};
+
+static int __init iphone_init(void)
+{
+ return usb_register(&iphone_driver);
+}
+
+static void __exit iphone_exit(void)
+{
+ usb_deregister(&iphone_driver);
+}
+
+module_init(iphone_init);
+module_exit(iphone_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Matt Colyer <[email protected]>");
+module_param(debug, bool, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(debug, "Debug enabled or not");


2007-08-24 07:21:01

by Oliver Neukum

[permalink] [raw]
Subject: Re: [RFC] USB: driver for iphone charging

Am Freitag 24 August 2007 schrieb Greg KH:

Hi,

> +static int select_configuration(struct usb_device *udev)
> +{
> +???????char *dummy_buffer = kzalloc(2, GFP_KERNEL);
> +???????int retval;
> +
> +???????if (!dummy_buffer)
> +???????????????return -ENOMEM;
> +
> +???????dbg(&udev->dev, "Calling set_configuration\n");
> +???????retval = usb_driver_set_configuration(udev, 3);
> +???????if (retval) {
> +???????????????dev_err(&udev->dev, "Set Configuration failed :%d.\n", retval);
> +???????????????goto exit;
> +???????}
> +
> +???????dbg(&udev->dev, "Sending first magic command\n");
> +???????retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x40, 0x40,
> +??????????????????????????????? 0x6400, 0, dummy_buffer, 0, 100);
> +

int usb_driver_set_configuration(struct usb_device *udev, int config)
{
struct set_config_request *req;

req = kmalloc(sizeof(*req), GFP_KERNEL);
if (!req)
return -ENOMEM;
req->udev = udev;
req->config = config;
INIT_WORK(&req->work, driver_set_config_work);

usb_get_dev(udev);
schedule_work(&req->work);
return 0;
}

This schedules the change via a workqueue, so you'll be reprobed. If you
fire of the first vendor command you are doing so before the configuration
is changed. How is this supposed to work?

Regards
Oliver

2007-08-24 10:51:49

by Bodo Eggert

[permalink] [raw]
Subject: Re: [RFC] USB: driver for iphone charging

Greg KH <[email protected]> wrote:

> my berry_charge code that adds support for charging the iphone when it
> is plugged into a Linux machine.

This should be a runtime option, because you may want to build a non-module
kernel and not charge the phone while running your laptop on battery.
--
Top 100 things you don't want the sysadmin to say:
72. My leave starts tomorrow.

Fri?, Spammer: [email protected]

2007-08-24 14:08:55

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, 24 Aug 2007, Oliver Neukum wrote:

> This schedules the change via a workqueue, so you'll be reprobed. If you
> fire of the first vendor command you are doing so before the configuration
> is changed. How is this supposed to work?

I would do it like this:

static int iphone_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(intf);
int rc;

if (udev->actconfig->desc.bConfigurationValue != 3) {
dbg(&udev->dev, "Calling set_configuration\n");
rc = usb_driver_set_configuration(udev, 3);
} else {
dbg(&udev->dev, "Configuration set, sending magic comand\n");
rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0x40, (USB_DIR_OUT | USB_TYPE_VENDOR |
USB_RECIP_DEVICE),
0x6400, 0, NULL, 0, 1000);
}
if (rc)
dev_err(&udev->dev, "Command failed: %d\n", rc);

/* we don't really want to bind to the device, userspace programs can
* handle the syncing just fine, so get outta here. */
return -ENODEV;
}

Alan Stern

2007-08-24 14:23:29

by Oliver Neukum

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

Am Freitag 24 August 2007 schrieb Alan Stern:
> On Fri, 24 Aug 2007, Oliver Neukum wrote:
>
> > This schedules the change via a workqueue, so you'll be reprobed. If you
> > fire of the first vendor command you are doing so before the configuration
> > is changed. How is this supposed to work?
>
> I would do it like this:

That makes sense. However, Greg's version might work by putting
out a magic init sequence and then changing the configuration.
Then it would just be coded in an obscure way.

However, does this really belong into kernel space? We have been
knowing that user space infrastructure for configuration selection
is necessary and this seems like a fine starting point.

Regards
Oliver

2007-08-24 18:35:24

by Greg KH

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, Aug 24, 2007 at 12:51:19PM +0200, Bodo Eggert wrote:
> Greg KH <[email protected]> wrote:
>
> > my berry_charge code that adds support for charging the iphone when it
> > is plugged into a Linux machine.
>
> This should be a runtime option, because you may want to build a non-module
> kernel and not charge the phone while running your laptop on battery.

Then just don't build this module if you are creating such a kernel :)

Same thing goes for the existing blackberry charge driver too...

thanks,

greg k-h

2007-08-24 18:38:20

by Greg KH

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, Aug 24, 2007 at 10:08:46AM -0400, Alan Stern wrote:
> On Fri, 24 Aug 2007, Oliver Neukum wrote:
>
> > This schedules the change via a workqueue, so you'll be reprobed. If you
> > fire of the first vendor command you are doing so before the configuration
> > is changed. How is this supposed to work?
>
> I would do it like this:
>
> static int iphone_probe(struct usb_interface *intf,
> const struct usb_device_id *id)
> {
> struct usb_device *udev = interface_to_usbdev(intf);
> int rc;
>
> if (udev->actconfig->desc.bConfigurationValue != 3) {
> dbg(&udev->dev, "Calling set_configuration\n");
> rc = usb_driver_set_configuration(udev, 3);
> } else {
> dbg(&udev->dev, "Configuration set, sending magic comand\n");
> rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
> 0x40, (USB_DIR_OUT | USB_TYPE_VENDOR |
> USB_RECIP_DEVICE),
> 0x6400, 0, NULL, 0, 1000);
> }
> if (rc)
> dev_err(&udev->dev, "Command failed: %d\n", rc);
>
> /* we don't really want to bind to the device, userspace programs can
> * handle the syncing just fine, so get outta here. */
> return -ENODEV;
> }

Yeah, that would make more sense, if that is what is needed.

Can someone with a iphone test this out? If you look at how the
berry_charge driver does it, the set_config stuff happens after the
magic command. As I don't have an iphone, nor have I ever seen any
dumps of the command streams, I don't really know if the set_config
message really is necessary or not.

thanks,

greg k-h

2007-08-24 18:38:36

by Greg KH

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, Aug 24, 2007 at 04:23:13PM +0200, Oliver Neukum wrote:
> Am Freitag 24 August 2007 schrieb Alan Stern:
> > On Fri, 24 Aug 2007, Oliver Neukum wrote:
> >
> > > This schedules the change via a workqueue, so you'll be reprobed. If you
> > > fire of the first vendor command you are doing so before the configuration
> > > is changed. How is this supposed to work?
> >
> > I would do it like this:
>
> That makes sense. However, Greg's version might work by putting
> out a magic init sequence and then changing the configuration.
> Then it would just be coded in an obscure way.

Without any docs, this is all obscure :)

> However, does this really belong into kernel space? We have been
> knowing that user space infrastructure for configuration selection
> is necessary and this seems like a fine starting point.

The berry_charge driver is also one that might be done in userspace, but
it turns out that people update their kernel much more than they do
userspace packages...

thanks,

greg k-h

2007-08-24 18:56:09

by Alan Stern

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, 24 Aug 2007, Greg KH wrote:

> Can someone with a iphone test this out? If you look at how the
> berry_charge driver does it, the set_config stuff happens after the
> magic command. As I don't have an iphone, nor have I ever seen any
> dumps of the command streams, I don't really know if the set_config
> message really is necessary or not.

You know, now that I think back on it, it may be that the Set-Config
really does have to come after the magic command. Perhaps it triggers
the changeover. In which case the subroutine should look like this:

static int iphone_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(intf);
int rc;

if (udev->actconfig->desc.bConfigurationValue != 3) {
dbg(&udev->dev, "Sending magic comand\n");
rc = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0x40, (USB_DIR_OUT | USB_TYPE_VENDOR |
USB_RECIP_DEVICE),
0x6400, 0, NULL, 0, 1000);
if (rc)
dev_err(&udev->dev, "Command failed: %d\n", rc);
else {
dbg(&udev->dev, "Calling set_configuration\n");
rc = usb_driver_set_configuration(udev, 3);
if (rc)
dev_err(&udev->dev, "Set-Config failed: %d\n",
rc);
}
}

/* we don't really want to bind to the device, userspace programs can
* handle the syncing just fine, so get outta here. */
return -ENODEV;
}

However Oliver's point is well taken. This simple sort of manipulation
could easily be done by a user program, started up by udev.

Alan Stern

2007-08-25 05:58:32

by Matt Colyer

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, 2007-08-24 at 14:55 -0400, Alan Stern wrote:
> On Fri, 24 Aug 2007, Greg KH wrote:
>
> > Can someone with a iphone test this out? If you look at how the
> > berry_charge driver does it, the set_config stuff happens after the
> > magic command. As I don't have an iphone, nor have I ever seen any
> > dumps of the command streams, I don't really know if the set_config
> > message really is necessary or not.
>
> You know, now that I think back on it, it may be that the Set-Config
> really does have to come after the magic command. Perhaps it triggers
> the changeover. In which case the subroutine should look like this:
>
Hi all,
I was the one who wrote the patch initially (sorry I haven't been able
to chime in sooner). It turns out that the configuration must be set
after the magic command (I tried both versions of the code tonight and
only the one which changes the configuration after the command works).
That said, I am not opposed to using a userspace program to accomplish
this task (I tried to pursue that avenue at first and couldn't find a
way to do, probably because it doesn't exist yet). I don't really have
much experience in this area, so I am not really sure how to proceed.

However the other part of this puzzle is that when you call lsusb the
iPhone disconnects from the bus. Greg suggested that it might be an
issue with the USB_SUSPEND support being broken on the iPhone and he
suggested I tried adding it to the quirks.c file. I'll be sure to let
you guys know when I figure it out.

-Matt


Attachments:
signature.asc (189.00 B)
This is a digitally signed message part

2007-08-25 08:52:19

by Bodo Eggert

[permalink] [raw]
Subject: Re: [linux-usb-devel] [RFC] USB: driver for iphone charging

On Fri, 24 Aug 2007, Greg KH wrote:
> On Fri, Aug 24, 2007 at 12:51:19PM +0200, Bodo Eggert wrote:
> > Greg KH <[email protected]> wrote:

> > > my berry_charge code that adds support for charging the iphone when it
> > > is plugged into a Linux machine.
> >
> > This should be a runtime option, because you may want to build a non-module
> > kernel and not charge the phone while running your laptop on battery.
>
> Then just don't build this module if you are creating such a kernel :)
>
> Same thing goes for the existing blackberry charge driver too...

So I have to reboot if I want to charge my (blackberry|iphone) on my
laptop?

Even if you say installing 850 KB of software (modutils) in order to
toggle charging an iphone is sane, having to (un)load a module is not
user-friendly.

--
"'Multiple exclamation marks,' he went on, shaking his head, 'are a
sure sign of a diseased mind.'"
-- Terry Pratchett in "Eric"