blob: 07a9dbb45c1c399936523eb3aeb72ee9b311f876 [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) {
Jack Phame8741502012-06-13 17:34:07 -0700142 pr_err("can't %s, device disconnected", opstr);
Jack Phamccbbfab2012-04-09 19:50:20 -0700143 return -ENODEV;
144 }
145
146 ret = usb_autopm_get_interface(hs->ifc);
147 if (ret < 0) {
Jack Phame8741502012-06-13 17:34:07 -0700148 dev_err(&hs->ifc->dev, "can't %s, autopm_get failed:%d\n",
Jack Phamccbbfab2012-04-09 19:50:20 -0700149 opstr, ret);
150 return ret;
151 }
152
153 atomic_inc(&hs->dbg_pending[op]);
154
155 ret = usb_bulk_msg(hs->udev, hs->pipe[op], data, len, actual_len,
156 timeout);
157
158 atomic_dec(&hs->dbg_pending[op]);
159
160 if (ret)
Jack Phame8741502012-06-13 17:34:07 -0700161 dev_err(&hs->ifc->dev,
Jack Phamccbbfab2012-04-09 19:50:20 -0700162 "can't %s, usb_bulk_msg failed, err:%d\n", opstr, ret);
163 else
164 atomic_add(*actual_len, &hs->dbg_bytecnt[op]);
165
166 usb_autopm_put_interface(hs->ifc);
167 return ret;
168}
169
170/**
171 * hsic_sysmon_read() - Read data from the HSIC sysmon interface.
172 * @id: the HSIC system monitor device to open
173 * @data: pointer to caller-allocated buffer to fill in
174 * @len: length in bytes of the buffer
175 * @actual_len: pointer to a location to put the actual length read
176 * in bytes
177 * @timeout: time in msecs to wait for the message to complete before
178 * timing out (if 0 the wait is forever)
179 *
180 * Context: !in_interrupt ()
181 *
182 * Synchronously reads data from the HSIC interface. The call will return
183 * after the read has completed, encountered an error, or timed out. Upon
184 * successful return actual_len will reflect the number of bytes read.
185 *
186 * If successful, it returns 0, otherwise a negative error number. The number
187 * of actual bytes transferred will be stored in the actual_len paramater.
188 */
189int hsic_sysmon_read(enum hsic_sysmon_device_id id, char *data, size_t len,
190 size_t *actual_len, int timeout)
191{
192 return hsic_sysmon_readwrite(id, data, len, actual_len,
193 timeout, HSIC_SYSMON_OP_READ);
194}
195EXPORT_SYMBOL(hsic_sysmon_read);
196
197/**
198 * hsic_sysmon_write() - Write data to the HSIC sysmon interface.
199 * @id: the HSIC system monitor device to open
200 * @data: pointer to caller-allocated buffer to write
201 * @len: length in bytes of the data in buffer to write
202 * @actual_len: pointer to a location to put the actual length written
203 * in bytes
204 * @timeout: time in msecs to wait for the message to complete before
205 * timing out (if 0 the wait is forever)
206 *
207 * Context: !in_interrupt ()
208 *
209 * Synchronously writes data to the HSIC interface. The call will return
210 * after the write has completed, encountered an error, or timed out. Upon
211 * successful return actual_len will reflect the number of bytes written.
212 *
213 * If successful, it returns 0, otherwise a negative error number. The number
214 * of actual bytes transferred will be stored in the actual_len paramater.
215 */
216int hsic_sysmon_write(enum hsic_sysmon_device_id id, const char *data,
217 size_t len, int timeout)
218{
219 size_t actual_len;
220 return hsic_sysmon_readwrite(id, (void *)data, len, &actual_len,
221 timeout, HSIC_SYSMON_OP_WRITE);
222}
223EXPORT_SYMBOL(hsic_sysmon_write);
224
225#if defined(CONFIG_DEBUG_FS)
226#define DEBUG_BUF_SIZE 512
227static ssize_t sysmon_debug_read_stats(struct file *file, char __user *ubuf,
228 size_t count, loff_t *ppos)
229{
230 char *buf;
231 int i, ret = 0;
232
233 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
234 if (!buf)
235 return -ENOMEM;
236
237 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
238 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
239 if (!hs)
240 continue;
241
242 ret += scnprintf(buf, DEBUG_BUF_SIZE,
243 "---HSIC Sysmon #%d---\n"
244 "epin:%d, epout:%d\n"
245 "bytes to host: %d\n"
246 "bytes to mdm: %d\n"
247 "pending reads: %d\n"
248 "pending writes: %d\n",
249 i, hs->in_epaddr & ~0x80, hs->out_epaddr,
250 atomic_read(
251 &hs->dbg_bytecnt[HSIC_SYSMON_OP_READ]),
252 atomic_read(
253 &hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE]),
254 atomic_read(
255 &hs->dbg_pending[HSIC_SYSMON_OP_READ]),
256 atomic_read(
257 &hs->dbg_pending[HSIC_SYSMON_OP_WRITE])
258 );
259 }
260
261 ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret);
262 kfree(buf);
263 return ret;
264}
265
266static ssize_t sysmon_debug_reset_stats(struct file *file,
267 const char __user *buf,
268 size_t count, loff_t *ppos)
269{
270 int i;
271
272 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
273 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
274 if (hs) {
275 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_READ], 0);
276 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE], 0);
277 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_READ], 0);
278 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_WRITE], 0);
279 }
280 }
281
282 return count;
283}
284
285const struct file_operations sysmon_stats_ops = {
286 .read = sysmon_debug_read_stats,
287 .write = sysmon_debug_reset_stats,
288};
289
290static struct dentry *dent;
291
292static void hsic_sysmon_debugfs_init(void)
293{
294 struct dentry *dfile;
295
296 dent = debugfs_create_dir("hsic_sysmon", 0);
297 if (IS_ERR(dent))
298 return;
299
300 dfile = debugfs_create_file("status", 0444, dent, 0, &sysmon_stats_ops);
301 if (!dfile || IS_ERR(dfile))
302 debugfs_remove(dent);
303}
304
305static void hsic_sysmon_debugfs_cleanup(void)
306{
307 if (dent) {
308 debugfs_remove_recursive(dent);
309 dent = NULL;
310 }
311}
312#else
313static inline void hsic_sysmon_debugfs_init(void) { }
314static inline void hsic_sysmon_debugfs_cleanup(void) { }
315#endif
316
317static int
318hsic_sysmon_probe(struct usb_interface *ifc, const struct usb_device_id *id)
319{
320 struct hsic_sysmon *hs;
321 struct usb_host_interface *ifc_desc;
322 struct usb_endpoint_descriptor *ep_desc;
323 int i;
324 int ret = -ENOMEM;
325 __u8 ifc_num;
326
327 pr_debug("id:%lu", id->driver_info);
328
329 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
330
331 /* is this the interface we're looking for? */
332 if (ifc_num != id->driver_info)
333 return -ENODEV;
334
335 hs = kzalloc(sizeof(*hs), GFP_KERNEL);
336 if (!hs) {
337 pr_err("unable to allocate hsic_sysmon");
338 return -ENOMEM;
339 }
340
341 hs->udev = usb_get_dev(interface_to_usbdev(ifc));
342 hs->ifc = ifc;
343 kref_init(&hs->kref);
344
345 ifc_desc = ifc->cur_altsetting;
346 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
347 ep_desc = &ifc_desc->endpoint[i].desc;
348
349 if (!hs->in_epaddr && usb_endpoint_is_bulk_in(ep_desc)) {
350 hs->in_epaddr = ep_desc->bEndpointAddress;
351 hs->pipe[HSIC_SYSMON_OP_READ] =
352 usb_rcvbulkpipe(hs->udev, hs->in_epaddr);
353 }
354
355 if (!hs->out_epaddr && usb_endpoint_is_bulk_out(ep_desc)) {
356 hs->out_epaddr = ep_desc->bEndpointAddress;
357 hs->pipe[HSIC_SYSMON_OP_WRITE] =
358 usb_sndbulkpipe(hs->udev, hs->out_epaddr);
359 }
360 }
361
362 if (!(hs->in_epaddr && hs->out_epaddr)) {
363 pr_err("could not find bulk in and bulk out endpoints");
364 ret = -ENODEV;
365 goto error;
366 }
367
368 hs->id = HSIC_SYSMON_DEV_EXT_MODEM;
369 hsic_sysmon_devices[HSIC_SYSMON_DEV_EXT_MODEM] = hs;
370 usb_set_intfdata(ifc, hs);
371
372 hs->pdev.name = "sys_mon";
373 hs->pdev.id = SYSMON_SS_EXT_MODEM;
374 platform_device_register(&hs->pdev);
375
376 pr_debug("complete");
377
378 return 0;
379
380error:
381 if (hs)
382 kref_put(&hs->kref, hsic_sysmon_delete);
383
384 return ret;
385}
386
387static void hsic_sysmon_disconnect(struct usb_interface *ifc)
388{
389 struct hsic_sysmon *hs = usb_get_intfdata(ifc);
390
391 platform_device_unregister(&hs->pdev);
392 kref_put(&hs->kref, hsic_sysmon_delete);
393 usb_set_intfdata(ifc, NULL);
394}
395
396static int hsic_sysmon_suspend(struct usb_interface *ifc, pm_message_t message)
397{
398 return 0;
399}
400
401static int hsic_sysmon_resume(struct usb_interface *ifc)
402{
403 return 0;
404}
405
406/* driver_info maps to the interface number corresponding to sysmon */
407static const struct usb_device_id hsic_sysmon_ids[] = {
408 { USB_DEVICE(0x5c6, 0x9048), .driver_info = 1, },
409 { USB_DEVICE(0x5c6, 0x904C), .driver_info = 1, },
410 {} /* terminating entry */
411};
412MODULE_DEVICE_TABLE(usb, hsic_sysmon_ids);
413
414static struct usb_driver hsic_sysmon_driver = {
415 .name = "hsic_sysmon",
416 .probe = hsic_sysmon_probe,
417 .disconnect = hsic_sysmon_disconnect,
418 .suspend = hsic_sysmon_suspend,
419 .resume = hsic_sysmon_resume,
420 .id_table = hsic_sysmon_ids,
421 .supports_autosuspend = 1,
422};
423
424static int __init hsic_sysmon_init(void)
425{
426 int ret;
427
428 ret = usb_register(&hsic_sysmon_driver);
429 if (ret) {
430 pr_err("unable to register " DRIVER_DESC);
431 return ret;
432 }
433
434 hsic_sysmon_debugfs_init();
435 return 0;
436}
437
438static void __exit hsic_sysmon_exit(void)
439{
440 hsic_sysmon_debugfs_cleanup();
441 usb_deregister(&hsic_sysmon_driver);
442}
443
444module_init(hsic_sysmon_init);
445module_exit(hsic_sysmon_exit);
446
447MODULE_DESCRIPTION(DRIVER_DESC);
448MODULE_LICENSE("GPL v2");