[4c6651]: drivers / serial / usbgecko.c Maximize Restore History

Download this file

usbgecko.c    598 lines (488 with data), 11.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* drivers/serial/usbgecko.c
*
* Console and TTY driver for the USB Gecko adapter.
* Copyright (C) 2008-2009 The GameCube Linux Team
* Copyright (C) 2008,2009 Albert Herranz
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
*/
#define UG_DEBUG
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/exi.h>
#define DRV_MODULE_NAME "usbgecko"
#define DRV_DESCRIPTION "Console and TTY driver for the USB Gecko adapter"
#define DRV_AUTHOR "Albert Herranz"
static char ug_driver_version[] = "0.1i";
#define drv_printk(level, format, arg...) \
printk(level DRV_MODULE_NAME ": " format , ## arg)
/*
*
* EXI related definitions.
*/
#define UG_SLOTA_CHANNEL 0 /* EXI0xxx */
#define UG_SLOTA_DEVICE 0 /* chip select, EXI0CSB0 */
#define UG_SLOTB_CHANNEL 1 /* EXI1xxx */
#define UG_SLOTB_DEVICE 0 /* chip select, EXI1CSB0 */
#define UG_SPI_CLK_IDX EXI_CLK_32MHZ
struct ug_adapter {
struct exi_device *exi_device;
struct task_struct *poller;
struct mutex mutex;
int refcnt;
};
static struct ug_adapter ug_adapters[2];
/*
*
* Hardware interface.
*/
/*
*
*/
static void ug_exi_io_transaction(struct exi_device *exi_device, u16 i, u16 *o)
{
u16 data;
exi_dev_select(exi_device);
data = i;
exi_dev_readwrite(exi_device, &data, 2);
exi_dev_deselect(exi_device);
*o = data;
}
#if 0
/*
*
*/
static void ug_io_transaction(struct ug_adapter *adapter, u16 i, u16 *o)
{
struct exi_device *exi_device = adapter->exi_device;
if (exi_device)
ug_exi_io_transaction(exi_device, i, o);
}
#endif
/*
*
*/
static int ug_check_adapter(struct exi_device *exi_device)
{
u16 data;
exi_dev_take(exi_device);
ug_exi_io_transaction(exi_device, 0x9000, &data);
exi_dev_give(exi_device);
return data == 0x0470;
}
#if 0
/*
*
*/
static int ug_is_txfifo_empty(struct ug_adapter *adapter)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xC000, &data);
exi_dev_give(exi_device);
return data & 0x0400;
}
return 0;
}
/*
*
*/
static int ug_is_rxfifo_empty(struct ug_adapter *adapter)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xD000, &data);
exi_dev_give(exi_device);
return data & 0x0400;
}
return 0;
}
/*
*
*/
static int ug_putc(struct ug_adapter *adapter, char c)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xB000|(c<<4), &data);
exi_dev_give(exi_device);
return data & 0x0400;
}
return 0;
}
/*
*
*/
static int ug_getc(struct ug_adapter *adapter, char *c)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xA000, &data);
exi_dev_give(exi_device);
if ((data & 0x0800)) {
*c = data & 0xff;
return 1;
}
}
return 0;
}
#endif
/*
*
*/
static int ug_safe_putc(struct ug_adapter *adapter, char c)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xC000, &data);
if ((data & 0x0400))
ug_exi_io_transaction(exi_device, 0xB000|(c<<4), &data);
exi_dev_give(exi_device);
return data & 0x0400;
}
return 0;
}
/*
*
*/
static int ug_safe_getc(struct ug_adapter *adapter, char *c)
{
struct exi_device *exi_device = adapter->exi_device;
u16 data;
if (!exi_device)
return 0;
if (!exi_dev_try_take(exi_device)) {
ug_exi_io_transaction(exi_device, 0xD000, &data);
if ((data & 0x0400)) {
ug_exi_io_transaction(exi_device, 0xA000, &data);
exi_dev_give(exi_device);
if ((data & 0x0800)) {
*c = data & 0xff;
return 1;
}
} else {
exi_dev_give(exi_device);
}
}
return 0;
}
/*
*
* Linux console interface.
*/
/*
*
*/
static void ug_console_write(struct console *co, const char *buf,
unsigned int count)
{
struct ug_adapter *adapter = co->data;
char *b = (char *)buf;
while (count--) {
if (*b == '\n')
ug_safe_putc(adapter, '\r');
ug_safe_putc(adapter, *b++);
}
}
/*
*
*/
static int ug_console_read(struct console *co, char *buf,
unsigned int count)
{
struct ug_adapter *adapter = co->data;
int i;
char c;
i = count;
while (i--) {
ug_safe_getc(adapter, &c);
*buf++ = c;
}
return count;
}
static struct tty_driver *ug_tty_driver;
static struct tty_driver *ug_console_device(struct console *co, int *index)
{
*index = co->index;
return ug_tty_driver;
}
static struct console ug_consoles[] = {
{
.name = DRV_MODULE_NAME "0",
.write = ug_console_write,
.read = ug_console_read,
.device = ug_console_device,
.flags = CON_PRINTBUFFER | CON_ENABLED,
.index = 0,
.data = &ug_adapters[0],
},
{
.name = DRV_MODULE_NAME "1",
.write = ug_console_write,
.read = ug_console_read,
.device = ug_console_device,
.flags = CON_PRINTBUFFER | CON_ENABLED,
.index = 1,
.data = &ug_adapters[1],
},
};
/*
*
* Linux tty driver.
*/
static int ug_tty_poller(void *tty_)
{
struct sched_param param = { .sched_priority = 1 };
struct tty_struct *tty = tty_;
struct ug_adapter *adapter;
int count, chunk;
const int max_outstanding = 32;
char ch;
sched_setscheduler(current, SCHED_FIFO, &param);
set_task_state(current, TASK_RUNNING);
chunk = 0;
while (!kthread_should_stop()) {
count = 0;
adapter = tty->driver_data;
if (adapter)
count = ug_safe_getc(adapter, &ch);
set_task_state(current, TASK_INTERRUPTIBLE);
if (count) {
tty_insert_flip_char(tty, ch, TTY_NORMAL);
if (chunk++ > max_outstanding) {
tty_flip_buffer_push(tty);
chunk = 0;
}
} else {
if (chunk) {
tty_flip_buffer_push(tty);
chunk = 0;
}
schedule_timeout(1);
}
set_task_state(current, TASK_RUNNING);
}
return 0;
}
static int ug_tty_open(struct tty_struct *tty, struct file *filp)
{
struct ug_adapter *adapter;
int index;
int retval = 0;
index = tty->index;
adapter = &ug_adapters[index];
mutex_lock(&adapter->mutex);
if (!adapter->exi_device) {
mutex_unlock(&adapter->mutex);
return -ENODEV;
}
if (!adapter->refcnt) {
adapter->poller = kthread_run(ug_tty_poller, tty, "kugtty");
if (IS_ERR(adapter->poller)) {
drv_printk(KERN_ERR, "error creating poller thread\n");
mutex_unlock(&adapter->mutex);
return -ENOMEM;
}
}
adapter->refcnt++;
tty->driver_data = adapter;
mutex_unlock(&adapter->mutex);
return retval;
}
static void ug_tty_close(struct tty_struct *tty, struct file *filp)
{
struct ug_adapter *adapter;
int index;
index = tty->index;
adapter = &ug_adapters[index];
mutex_lock(&adapter->mutex);
adapter->refcnt--;
if (!adapter->refcnt) {
if (!IS_ERR(adapter->poller))
kthread_stop(adapter->poller);
adapter->poller = ERR_PTR(-EINVAL);
tty->driver_data = NULL;
}
mutex_unlock(&adapter->mutex);
}
static int ug_tty_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
struct ug_adapter *adapter = tty->driver_data;
char *b = (char *)buf;
int index;
int i;
if (!adapter)
return -ENODEV;
index = tty->index;
adapter = &ug_adapters[index];
for (i = 0; i < count; i++)
ug_safe_putc(adapter, *b++);
return count;
}
static int ug_tty_write_room(struct tty_struct *tty)
{
return 0x123; /* whatever */
}
static int ug_tty_chars_in_buffer(struct tty_struct *tty)
{
return 0; /* unbuffered */
}
static const struct tty_operations ug_tty_ops = {
.open = ug_tty_open,
.close = ug_tty_close,
.write = ug_tty_write,
.write_room = ug_tty_write_room,
.chars_in_buffer = ug_tty_chars_in_buffer,
};
static int ug_tty_init(void)
{
struct tty_driver *driver;
int retval;
driver = alloc_tty_driver(2);
if (!driver)
return -ENOMEM;
driver->name = DRV_MODULE_NAME "con";
driver->major = TTY_MAJOR;
driver->minor_start = 64;
driver->type = TTY_DRIVER_TYPE_SYSCONS;
driver->init_termios = tty_std_termios;
tty_set_operations(driver, &ug_tty_ops);
retval = tty_register_driver(driver);
if (retval) {
put_tty_driver(driver);
return retval;
}
ug_tty_driver = driver;
return 0;
}
static void ug_tty_exit(void)
{
struct tty_driver *driver = ug_tty_driver;
ug_tty_driver = NULL;
if (driver) {
tty_unregister_driver(driver);
put_tty_driver(driver);
}
}
/*
*
* EXI layer interface.
*/
/*
*
*/
static int ug_probe(struct exi_device *exi_device)
{
struct console *console;
struct ug_adapter *adapter;
unsigned int slot;
/* don't try to drive a device which already has a real identifier */
if (exi_device->eid.id != EXI_ID_NONE)
return -ENODEV;
if (!ug_check_adapter(exi_device))
return -ENODEV;
slot = to_channel(exi_get_exi_channel(exi_device));
console = &ug_consoles[slot];
adapter = console->data;
drv_printk(KERN_INFO, "USB Gecko detected in memcard slot-%c\n",
'A'+slot);
adapter->poller = ERR_PTR(-EINVAL);
mutex_init(&adapter->mutex);
adapter->refcnt = 0;
adapter->exi_device = exi_device_get(exi_device);
exi_set_drvdata(exi_device, adapter);
register_console(console);
ug_tty_init();
return 0;
}
/*
* Makes unavailable the USB Gecko adapter identified by the EXI device
* `exi_device'.
*/
static void ug_remove(struct exi_device *exi_device)
{
struct console *console;
struct ug_adapter *adapter;
unsigned int slot;
slot = to_channel(exi_get_exi_channel(exi_device));
console = &ug_consoles[slot];
adapter = console->data;
if (adapter->refcnt)
drv_printk(KERN_ERR, "adapter removed while in use!\n");
ug_tty_exit();
unregister_console(console);
exi_set_drvdata(exi_device, NULL);
adapter->exi_device = NULL;
exi_device_put(exi_device);
mutex_destroy(&adapter->mutex);
drv_printk(KERN_INFO, "USB Gecko removed from memcard slot-%c\n",
'A'+slot);
}
static struct exi_device_id ug_eid_table[] = {
[0] = {
.channel = UG_SLOTA_CHANNEL,
.device = UG_SLOTA_DEVICE,
.id = EXI_ID_NONE,
},
[1] = {
.channel = UG_SLOTB_CHANNEL,
.device = UG_SLOTB_DEVICE,
.id = EXI_ID_NONE,
},
{.id = 0}
};
static struct exi_driver ug_exi_driver = {
.name = DRV_MODULE_NAME,
.eid_table = ug_eid_table,
.frequency = UG_SPI_CLK_IDX,
.probe = ug_probe,
.remove = ug_remove,
};
/*
*
* Module interface.
*/
static int __init ug_init_module(void)
{
drv_printk(KERN_INFO, "%s - version %s\n", DRV_DESCRIPTION,
ug_driver_version);
return exi_driver_register(&ug_exi_driver);
}
static void __exit ug_exit_module(void)
{
exi_driver_unregister(&ug_exi_driver);
}
module_init(ug_init_module);
module_exit(ug_exit_module);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_LICENSE("GPL");