blob: 2dedbacc8c5eec98f222cef369abd449025c1fa1 [file] [log] [blame]
Jack Phamccbbfab2012-04-09 19:50:20 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13/* add additional information to our printk's */
14#define pr_fmt(fmt) "%s: " fmt "\n", __func__
15
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/module.h>
21#include <linux/kref.h>
22#include <linux/platform_device.h>
23#include <linux/uaccess.h>
24#include <linux/usb.h>
25#include <linux/debugfs.h>
26
27#include "hsic_sysmon.h"
28#include "sysmon.h"
29
30#define DRIVER_DESC "HSIC System monitor driver"
31
32enum hsic_sysmon_op {
33 HSIC_SYSMON_OP_READ = 0,
34 HSIC_SYSMON_OP_WRITE,
35 NUM_OPS
36};
37
38struct hsic_sysmon {
39 struct usb_device *udev;
40 struct usb_interface *ifc;
41 __u8 in_epaddr;
42 __u8 out_epaddr;
43 unsigned int pipe[NUM_OPS];
44 struct kref kref;
45 struct platform_device pdev;
46 int id;
47
48 /* debugging counters */
49 atomic_t dbg_bytecnt[NUM_OPS];
50 atomic_t dbg_pending[NUM_OPS];
51};
52static struct hsic_sysmon *hsic_sysmon_devices[NUM_HSIC_SYSMON_DEVS];
53
54static void hsic_sysmon_delete(struct kref *kref)
55{
56 struct hsic_sysmon *hs = container_of(kref, struct hsic_sysmon, kref);
57
58 usb_put_dev(hs->udev);
59 hsic_sysmon_devices[hs->id] = NULL;
60 kfree(hs);
61}
62
63/**
64 * hsic_sysmon_open() - Opens the system monitor bridge.
65 * @id: the HSIC system monitor device to open
66 *
67 * This should only be called after the platform_device "sys_mon" with id
68 * SYSMON_SS_EXT_MODEM has been added. The simplest way to do that is to
69 * register a platform_driver and its probe will be called when the HSIC
70 * device is ready.
71 */
72int hsic_sysmon_open(enum hsic_sysmon_device_id id)
73{
74 struct hsic_sysmon *hs;
75
76 if (id >= NUM_HSIC_SYSMON_DEVS) {
77 pr_err("invalid dev id(%d)", id);
78 return -ENODEV;
79 }
80
81 hs = hsic_sysmon_devices[id];
82 if (!hs) {
83 pr_err("dev is null");
84 return -ENODEV;
85 }
86
87 kref_get(&hs->kref);
88
89 return 0;
90}
91EXPORT_SYMBOL(hsic_sysmon_open);
92
93/**
94 * hsic_sysmon_close() - Closes the system monitor bridge.
95 * @id: the HSIC system monitor device to close
96 */
97void hsic_sysmon_close(enum hsic_sysmon_device_id id)
98{
99 struct hsic_sysmon *hs;
100
101 if (id >= NUM_HSIC_SYSMON_DEVS) {
102 pr_err("invalid dev id(%d)", id);
103 return;
104 }
105
106 hs = hsic_sysmon_devices[id];
107 kref_put(&hs->kref, hsic_sysmon_delete);
108}
109EXPORT_SYMBOL(hsic_sysmon_close);
110
111/**
112 * hsic_sysmon_readwrite() - Common function to send read/write over HSIC
113 */
114static int hsic_sysmon_readwrite(enum hsic_sysmon_device_id id, void *data,
115 size_t len, size_t *actual_len, int timeout,
116 enum hsic_sysmon_op op)
117{
118 struct hsic_sysmon *hs;
119 int ret;
120 const char *opstr = (op == HSIC_SYSMON_OP_READ) ?
121 "read" : "write";
122
123 pr_debug("%s: id:%d, data len:%d, timeout:%d", opstr, id, len, timeout);
124
125 if (id >= NUM_HSIC_SYSMON_DEVS) {
126 pr_err("invalid dev id(%d)", id);
127 return -ENODEV;
128 }
129
130 if (!len) {
131 pr_err("length(%d) must be greater than 0", len);
132 return -EINVAL;
133 }
134
135 hs = hsic_sysmon_devices[id];
136 if (!hs) {
137 pr_err("device was not opened");
138 return -ENODEV;
139 }
140
141 if (!hs->ifc) {
142 dev_err(&hs->udev->dev, "can't %s, device disconnected\n",
143 opstr);
144 return -ENODEV;
145 }
146
147 ret = usb_autopm_get_interface(hs->ifc);
148 if (ret < 0) {
149 dev_err(&hs->udev->dev, "can't %s, autopm_get failed:%d\n",
150 opstr, ret);
151 return ret;
152 }
153
154 atomic_inc(&hs->dbg_pending[op]);
155
156 ret = usb_bulk_msg(hs->udev, hs->pipe[op], data, len, actual_len,
157 timeout);
158
159 atomic_dec(&hs->dbg_pending[op]);
160
161 if (ret)
162 dev_err(&hs->udev->dev,
163 "can't %s, usb_bulk_msg failed, err:%d\n", opstr, ret);
164 else
165 atomic_add(*actual_len, &hs->dbg_bytecnt[op]);
166
167 usb_autopm_put_interface(hs->ifc);
168 return ret;
169}
170
171/**
172 * hsic_sysmon_read() - Read data from the HSIC sysmon interface.
173 * @id: the HSIC system monitor device to open
174 * @data: pointer to caller-allocated buffer to fill in
175 * @len: length in bytes of the buffer
176 * @actual_len: pointer to a location to put the actual length read
177 * in bytes
178 * @timeout: time in msecs to wait for the message to complete before
179 * timing out (if 0 the wait is forever)
180 *
181 * Context: !in_interrupt ()
182 *
183 * Synchronously reads data from the HSIC interface. The call will return
184 * after the read has completed, encountered an error, or timed out. Upon
185 * successful return actual_len will reflect the number of bytes read.
186 *
187 * If successful, it returns 0, otherwise a negative error number. The number
188 * of actual bytes transferred will be stored in the actual_len paramater.
189 */
190int hsic_sysmon_read(enum hsic_sysmon_device_id id, char *data, size_t len,
191 size_t *actual_len, int timeout)
192{
193 return hsic_sysmon_readwrite(id, data, len, actual_len,
194 timeout, HSIC_SYSMON_OP_READ);
195}
196EXPORT_SYMBOL(hsic_sysmon_read);
197
198/**
199 * hsic_sysmon_write() - Write data to the HSIC sysmon interface.
200 * @id: the HSIC system monitor device to open
201 * @data: pointer to caller-allocated buffer to write
202 * @len: length in bytes of the data in buffer to write
203 * @actual_len: pointer to a location to put the actual length written
204 * in bytes
205 * @timeout: time in msecs to wait for the message to complete before
206 * timing out (if 0 the wait is forever)
207 *
208 * Context: !in_interrupt ()
209 *
210 * Synchronously writes data to the HSIC interface. The call will return
211 * after the write has completed, encountered an error, or timed out. Upon
212 * successful return actual_len will reflect the number of bytes written.
213 *
214 * If successful, it returns 0, otherwise a negative error number. The number
215 * of actual bytes transferred will be stored in the actual_len paramater.
216 */
217int hsic_sysmon_write(enum hsic_sysmon_device_id id, const char *data,
218 size_t len, int timeout)
219{
220 size_t actual_len;
221 return hsic_sysmon_readwrite(id, (void *)data, len, &actual_len,
222 timeout, HSIC_SYSMON_OP_WRITE);
223}
224EXPORT_SYMBOL(hsic_sysmon_write);
225
226#if defined(CONFIG_DEBUG_FS)
227#define DEBUG_BUF_SIZE 512
228static ssize_t sysmon_debug_read_stats(struct file *file, char __user *ubuf,
229 size_t count, loff_t *ppos)
230{
231 char *buf;
232 int i, ret = 0;
233
234 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
235 if (!buf)
236 return -ENOMEM;
237
238 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
239 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
240 if (!hs)
241 continue;
242
243 ret += scnprintf(buf, DEBUG_BUF_SIZE,
244 "---HSIC Sysmon #%d---\n"
245 "epin:%d, epout:%d\n"
246 "bytes to host: %d\n"
247 "bytes to mdm: %d\n"
248 "pending reads: %d\n"
249 "pending writes: %d\n",
250 i, hs->in_epaddr & ~0x80, hs->out_epaddr,
251 atomic_read(
252 &hs->dbg_bytecnt[HSIC_SYSMON_OP_READ]),
253 atomic_read(
254 &hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE]),
255 atomic_read(
256 &hs->dbg_pending[HSIC_SYSMON_OP_READ]),
257 atomic_read(
258 &hs->dbg_pending[HSIC_SYSMON_OP_WRITE])
259 );
260 }
261
262 ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret);
263 kfree(buf);
264 return ret;
265}
266
267static ssize_t sysmon_debug_reset_stats(struct file *file,
268 const char __user *buf,
269 size_t count, loff_t *ppos)
270{
271 int i;
272
273 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
274 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
275 if (hs) {
276 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_READ], 0);
277 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE], 0);
278 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_READ], 0);
279 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_WRITE], 0);
280 }
281 }
282
283 return count;
284}
285
286const struct file_operations sysmon_stats_ops = {
287 .read = sysmon_debug_read_stats,
288 .write = sysmon_debug_reset_stats,
289};
290
291static struct dentry *dent;
292
293static void hsic_sysmon_debugfs_init(void)
294{
295 struct dentry *dfile;
296
297 dent = debugfs_create_dir("hsic_sysmon", 0);
298 if (IS_ERR(dent))
299 return;
300
301 dfile = debugfs_create_file("status", 0444, dent, 0, &sysmon_stats_ops);
302 if (!dfile || IS_ERR(dfile))
303 debugfs_remove(dent);
304}
305
306static void hsic_sysmon_debugfs_cleanup(void)
307{
308 if (dent) {
309 debugfs_remove_recursive(dent);
310 dent = NULL;
311 }
312}
313#else
314static inline void hsic_sysmon_debugfs_init(void) { }
315static inline void hsic_sysmon_debugfs_cleanup(void) { }
316#endif
317
318static int
319hsic_sysmon_probe(struct usb_interface *ifc, const struct usb_device_id *id)
320{
321 struct hsic_sysmon *hs;
322 struct usb_host_interface *ifc_desc;
323 struct usb_endpoint_descriptor *ep_desc;
324 int i;
325 int ret = -ENOMEM;
326 __u8 ifc_num;
327
328 pr_debug("id:%lu", id->driver_info);
329
330 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
331
332 /* is this the interface we're looking for? */
333 if (ifc_num != id->driver_info)
334 return -ENODEV;
335
336 hs = kzalloc(sizeof(*hs), GFP_KERNEL);
337 if (!hs) {
338 pr_err("unable to allocate hsic_sysmon");
339 return -ENOMEM;
340 }
341
342 hs->udev = usb_get_dev(interface_to_usbdev(ifc));
343 hs->ifc = ifc;
344 kref_init(&hs->kref);
345
346 ifc_desc = ifc->cur_altsetting;
347 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
348 ep_desc = &ifc_desc->endpoint[i].desc;
349
350 if (!hs->in_epaddr && usb_endpoint_is_bulk_in(ep_desc)) {
351 hs->in_epaddr = ep_desc->bEndpointAddress;
352 hs->pipe[HSIC_SYSMON_OP_READ] =
353 usb_rcvbulkpipe(hs->udev, hs->in_epaddr);
354 }
355
356 if (!hs->out_epaddr && usb_endpoint_is_bulk_out(ep_desc)) {
357 hs->out_epaddr = ep_desc->bEndpointAddress;
358 hs->pipe[HSIC_SYSMON_OP_WRITE] =
359 usb_sndbulkpipe(hs->udev, hs->out_epaddr);
360 }
361 }
362
363 if (!(hs->in_epaddr && hs->out_epaddr)) {
364 pr_err("could not find bulk in and bulk out endpoints");
365 ret = -ENODEV;
366 goto error;
367 }
368
369 hs->id = HSIC_SYSMON_DEV_EXT_MODEM;
370 hsic_sysmon_devices[HSIC_SYSMON_DEV_EXT_MODEM] = hs;
371 usb_set_intfdata(ifc, hs);
372
373 hs->pdev.name = "sys_mon";
374 hs->pdev.id = SYSMON_SS_EXT_MODEM;
375 platform_device_register(&hs->pdev);
376
377 pr_debug("complete");
378
379 return 0;
380
381error:
382 if (hs)
383 kref_put(&hs->kref, hsic_sysmon_delete);
384
385 return ret;
386}
387
388static void hsic_sysmon_disconnect(struct usb_interface *ifc)
389{
390 struct hsic_sysmon *hs = usb_get_intfdata(ifc);
391
392 platform_device_unregister(&hs->pdev);
393 kref_put(&hs->kref, hsic_sysmon_delete);
394 usb_set_intfdata(ifc, NULL);
395}
396
397static int hsic_sysmon_suspend(struct usb_interface *ifc, pm_message_t message)
398{
399 return 0;
400}
401
402static int hsic_sysmon_resume(struct usb_interface *ifc)
403{
404 return 0;
405}
406
407/* driver_info maps to the interface number corresponding to sysmon */
408static const struct usb_device_id hsic_sysmon_ids[] = {
409 { USB_DEVICE(0x5c6, 0x9048), .driver_info = 1, },
410 { USB_DEVICE(0x5c6, 0x904C), .driver_info = 1, },
411 {} /* terminating entry */
412};
413MODULE_DEVICE_TABLE(usb, hsic_sysmon_ids);
414
415static struct usb_driver hsic_sysmon_driver = {
416 .name = "hsic_sysmon",
417 .probe = hsic_sysmon_probe,
418 .disconnect = hsic_sysmon_disconnect,
419 .suspend = hsic_sysmon_suspend,
420 .resume = hsic_sysmon_resume,
421 .id_table = hsic_sysmon_ids,
422 .supports_autosuspend = 1,
423};
424
425static int __init hsic_sysmon_init(void)
426{
427 int ret;
428
429 ret = usb_register(&hsic_sysmon_driver);
430 if (ret) {
431 pr_err("unable to register " DRIVER_DESC);
432 return ret;
433 }
434
435 hsic_sysmon_debugfs_init();
436 return 0;
437}
438
439static void __exit hsic_sysmon_exit(void)
440{
441 hsic_sysmon_debugfs_cleanup();
442 usb_deregister(&hsic_sysmon_driver);
443}
444
445module_init(hsic_sysmon_init);
446module_exit(hsic_sysmon_exit);
447
448MODULE_DESCRIPTION(DRIVER_DESC);
449MODULE_LICENSE("GPL v2");