blob: 92c1d2768df9680afb7050d74b09b30c10db1d65 [file] [log] [blame]
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -08001/*
2 * USB BlackBerry charging module
3 *
4 * Copyright (C) 2007 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
9 *
10 * Information on how to switch configs was taken by the bcharge.cc file
11 * created by the barry.sf.net project.
12 *
13 * bcharge.cc has the following copyright:
14 * Copyright (C) 2006, Net Direct Inc. (http://www.netdirect.ca/)
15 * and is released under the GPLv2.
16 *
17 *
18 */
19
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/usb.h>
26
27#define RIM_VENDOR 0x0fca
28#define BLACKBERRY 0x0001
Jeremy Katz49bb6072007-06-19 17:15:38 -040029#define BLACKBERRY_PEARL_DUAL 0x0004
30#define BLACKBERRY_PEARL 0x0006
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -080031
32static int debug;
Jeremy Katz49bb6072007-06-19 17:15:38 -040033static int pearl_dual_mode = 1;
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -080034
35#ifdef dbg
36#undef dbg
37#endif
38#define dbg(dev, format, arg...) \
39 if (debug) \
40 dev_printk(KERN_DEBUG , dev , format , ## arg)
41
42static struct usb_device_id id_table [] = {
43 { USB_DEVICE(RIM_VENDOR, BLACKBERRY) },
Jeremy Katz49bb6072007-06-19 17:15:38 -040044 { USB_DEVICE(RIM_VENDOR, BLACKBERRY_PEARL) },
45 { USB_DEVICE(RIM_VENDOR, BLACKBERRY_PEARL_DUAL) },
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -080046 { }, /* Terminating entry */
47};
48MODULE_DEVICE_TABLE(usb, id_table);
49
50static int magic_charge(struct usb_device *udev)
51{
52 char *dummy_buffer = kzalloc(2, GFP_KERNEL);
53 int retval;
54
55 if (!dummy_buffer)
56 return -ENOMEM;
57
58 /* send two magic commands and then set the configuration. The device
59 * will then reset itself with the new power usage and should start
60 * charging. */
61
62 /* Note, with testing, it only seems that the first message is really
63 * needed (at least for the 8700c), but to be safe, we emulate what
64 * other operating systems seem to be sending to their device. We
65 * really need to get some specs for this device to be sure about what
66 * is going on here.
67 */
68 dbg(&udev->dev, "Sending first magic command\n");
69 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
70 0xa5, 0xc0, 0, 1, dummy_buffer, 2, 100);
71 if (retval != 2) {
72 dev_err(&udev->dev, "First magic command failed: %d.\n",
73 retval);
74 return retval;
75 }
76
Ken L Johnson774f78c2007-03-16 10:17:31 -060077 dbg(&udev->dev, "Sending second magic command\n");
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -080078 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
79 0xa2, 0x40, 0, 1, dummy_buffer, 0, 100);
80 if (retval != 0) {
81 dev_err(&udev->dev, "Second magic command failed: %d.\n",
82 retval);
83 return retval;
84 }
85
86 dbg(&udev->dev, "Calling set_configuration\n");
87 retval = usb_driver_set_configuration(udev, 1);
88 if (retval)
89 dev_err(&udev->dev, "Set Configuration failed :%d.\n", retval);
90
91 return retval;
92}
93
Jeremy Katz49bb6072007-06-19 17:15:38 -040094static int magic_dual_mode(struct usb_device *udev)
95{
96 char *dummy_buffer = kzalloc(2, GFP_KERNEL);
97 int retval;
98
99 if (!dummy_buffer)
100 return -ENOMEM;
101
102 /* send magic command so that the Blackberry Pearl device exposes
103 * two interfaces: both the USB mass-storage one and one which can
104 * be used for database access. */
105 dbg(&udev->dev, "Sending magic pearl command\n");
106 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
107 0xa9, 0xc0, 1, 1, dummy_buffer, 2, 100);
108 dbg(&udev->dev, "Magic pearl command returned %d\n", retval);
109
110 dbg(&udev->dev, "Calling set_configuration\n");
111 retval = usb_driver_set_configuration(udev, 1);
112 if (retval)
113 dev_err(&udev->dev, "Set Configuration failed :%d.\n", retval);
114
115 return retval;
116}
117
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -0800118static int berry_probe(struct usb_interface *intf,
119 const struct usb_device_id *id)
120{
121 struct usb_device *udev = interface_to_usbdev(intf);
122
123 dbg(&udev->dev, "Power is set to %dmA\n",
124 udev->actconfig->desc.bMaxPower * 2);
125
126 /* check the power usage so we don't try to enable something that is
127 * already enabled */
128 if ((udev->actconfig->desc.bMaxPower * 2) == 500) {
129 dbg(&udev->dev, "device is already charging, power is "
130 "set to %dmA\n", udev->actconfig->desc.bMaxPower * 2);
131 return -ENODEV;
132 }
133
134 /* turn the power on */
135 magic_charge(udev);
136
Jeremy Katz49bb6072007-06-19 17:15:38 -0400137 if ((le16_to_cpu(udev->descriptor.idProduct) == BLACKBERRY_PEARL) &&
138 (pearl_dual_mode))
139 magic_dual_mode(udev);
140
Greg Kroah-Hartmandf23fa02007-01-13 10:57:42 -0800141 /* we don't really want to bind to the device, userspace programs can
142 * handle the syncing just fine, so get outta here. */
143 return -ENODEV;
144}
145
146static void berry_disconnect(struct usb_interface *intf)
147{
148}
149
150static struct usb_driver berry_driver = {
151 .name = "berry_charge",
152 .probe = berry_probe,
153 .disconnect = berry_disconnect,
154 .id_table = id_table,
155};
156
157static int __init berry_init(void)
158{
159 return usb_register(&berry_driver);
160}
161
162static void __exit berry_exit(void)
163{
164 usb_deregister(&berry_driver);
165}
166
167module_init(berry_init);
168module_exit(berry_exit);
169
170MODULE_LICENSE("GPL");
171MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
172module_param(debug, bool, S_IRUGO | S_IWUSR);
173MODULE_PARM_DESC(debug, "Debug enabled or not");
Jeremy Katz49bb6072007-06-19 17:15:38 -0400174module_param(pearl_dual_mode, bool, S_IRUGO | S_IWUSR);
175MODULE_PARM_DESC(pearl_dual_mode, "Change Blackberry Pearl to run in dual mode");