common.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/ctype.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  21. #define DRIVER_DESC "Driver for Gigaset 307x"
  22. #ifdef CONFIG_GIGASET_DEBUG
  23. #define DRIVER_DESC_DEBUG " (debug build)"
  24. #else
  25. #define DRIVER_DESC_DEBUG ""
  26. #endif
  27. /* Module parameters */
  28. int gigaset_debuglevel = DEBUG_DEFAULT;
  29. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  30. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
  31. MODULE_PARM_DESC(debug, "debug level");
  32. /* driver state flags */
  33. #define VALID_MINOR 0x01
  34. #define VALID_ID 0x02
  35. /**
  36. * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
  37. * @level: debugging level.
  38. * @msg: message prefix.
  39. * @len: number of bytes to dump.
  40. * @buf: data to dump.
  41. *
  42. * If the current debugging level includes one of the bits set in @level,
  43. * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
  44. * prefixed by the text @msg.
  45. */
  46. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  47. size_t len, const unsigned char *buf)
  48. {
  49. unsigned char outbuf[80];
  50. unsigned char c;
  51. size_t space = sizeof outbuf - 1;
  52. unsigned char *out = outbuf;
  53. size_t numin = len;
  54. while (numin--) {
  55. c = *buf++;
  56. if (c == '~' || c == '^' || c == '\\') {
  57. if (!space--)
  58. break;
  59. *out++ = '\\';
  60. }
  61. if (c & 0x80) {
  62. if (!space--)
  63. break;
  64. *out++ = '~';
  65. c ^= 0x80;
  66. }
  67. if (c < 0x20 || c == 0x7f) {
  68. if (!space--)
  69. break;
  70. *out++ = '^';
  71. c ^= 0x40;
  72. }
  73. if (!space--)
  74. break;
  75. *out++ = c;
  76. }
  77. *out = 0;
  78. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  79. }
  80. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  81. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  82. {
  83. int r;
  84. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  85. cs->control_state = flags;
  86. if (r < 0)
  87. return r;
  88. if (delay) {
  89. set_current_state(TASK_INTERRUPTIBLE);
  90. schedule_timeout(delay * HZ / 1000);
  91. }
  92. return 0;
  93. }
  94. int gigaset_enterconfigmode(struct cardstate *cs)
  95. {
  96. int i, r;
  97. cs->control_state = TIOCM_RTS; //FIXME
  98. r = setflags(cs, TIOCM_DTR, 200);
  99. if (r < 0)
  100. goto error;
  101. r = setflags(cs, 0, 200);
  102. if (r < 0)
  103. goto error;
  104. for (i = 0; i < 5; ++i) {
  105. r = setflags(cs, TIOCM_RTS, 100);
  106. if (r < 0)
  107. goto error;
  108. r = setflags(cs, 0, 100);
  109. if (r < 0)
  110. goto error;
  111. }
  112. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  113. if (r < 0)
  114. goto error;
  115. return 0;
  116. error:
  117. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  118. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  119. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  120. return -1; //r
  121. }
  122. static int test_timeout(struct at_state_t *at_state)
  123. {
  124. if (!at_state->timer_expires)
  125. return 0;
  126. if (--at_state->timer_expires) {
  127. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  128. at_state, at_state->timer_expires);
  129. return 0;
  130. }
  131. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  132. at_state->timer_index, NULL)) {
  133. //FIXME what should we do?
  134. }
  135. return 1;
  136. }
  137. static void timer_tick(unsigned long data)
  138. {
  139. struct cardstate *cs = (struct cardstate *) data;
  140. unsigned long flags;
  141. unsigned channel;
  142. struct at_state_t *at_state;
  143. int timeout = 0;
  144. spin_lock_irqsave(&cs->lock, flags);
  145. for (channel = 0; channel < cs->channels; ++channel)
  146. if (test_timeout(&cs->bcs[channel].at_state))
  147. timeout = 1;
  148. if (test_timeout(&cs->at_state))
  149. timeout = 1;
  150. list_for_each_entry(at_state, &cs->temp_at_states, list)
  151. if (test_timeout(at_state))
  152. timeout = 1;
  153. if (cs->running) {
  154. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  155. if (timeout) {
  156. gig_dbg(DEBUG_CMD, "scheduling timeout");
  157. tasklet_schedule(&cs->event_tasklet);
  158. }
  159. }
  160. spin_unlock_irqrestore(&cs->lock, flags);
  161. }
  162. int gigaset_get_channel(struct bc_state *bcs)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&bcs->cs->lock, flags);
  166. if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
  167. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  168. bcs->channel);
  169. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  170. return 0;
  171. }
  172. ++bcs->use_count;
  173. bcs->busy = 1;
  174. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  175. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  176. return 1;
  177. }
  178. struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
  179. {
  180. unsigned long flags;
  181. int i;
  182. spin_lock_irqsave(&cs->lock, flags);
  183. if (!try_module_get(cs->driver->owner)) {
  184. gig_dbg(DEBUG_ANY,
  185. "could not get module for allocating channel");
  186. spin_unlock_irqrestore(&cs->lock, flags);
  187. return NULL;
  188. }
  189. for (i = 0; i < cs->channels; ++i)
  190. if (!cs->bcs[i].use_count) {
  191. ++cs->bcs[i].use_count;
  192. cs->bcs[i].busy = 1;
  193. spin_unlock_irqrestore(&cs->lock, flags);
  194. gig_dbg(DEBUG_ANY, "allocated channel %d", i);
  195. return cs->bcs + i;
  196. }
  197. module_put(cs->driver->owner);
  198. spin_unlock_irqrestore(&cs->lock, flags);
  199. gig_dbg(DEBUG_ANY, "no free channel");
  200. return NULL;
  201. }
  202. void gigaset_free_channel(struct bc_state *bcs)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&bcs->cs->lock, flags);
  206. if (!bcs->busy) {
  207. gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
  208. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  209. return;
  210. }
  211. --bcs->use_count;
  212. bcs->busy = 0;
  213. module_put(bcs->cs->driver->owner);
  214. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  215. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  216. }
  217. int gigaset_get_channels(struct cardstate *cs)
  218. {
  219. unsigned long flags;
  220. int i;
  221. spin_lock_irqsave(&cs->lock, flags);
  222. for (i = 0; i < cs->channels; ++i)
  223. if (cs->bcs[i].use_count) {
  224. spin_unlock_irqrestore(&cs->lock, flags);
  225. gig_dbg(DEBUG_ANY, "could not allocate all channels");
  226. return 0;
  227. }
  228. for (i = 0; i < cs->channels; ++i)
  229. ++cs->bcs[i].use_count;
  230. spin_unlock_irqrestore(&cs->lock, flags);
  231. gig_dbg(DEBUG_ANY, "allocated all channels");
  232. return 1;
  233. }
  234. void gigaset_free_channels(struct cardstate *cs)
  235. {
  236. unsigned long flags;
  237. int i;
  238. gig_dbg(DEBUG_ANY, "unblocking all channels");
  239. spin_lock_irqsave(&cs->lock, flags);
  240. for (i = 0; i < cs->channels; ++i)
  241. --cs->bcs[i].use_count;
  242. spin_unlock_irqrestore(&cs->lock, flags);
  243. }
  244. void gigaset_block_channels(struct cardstate *cs)
  245. {
  246. unsigned long flags;
  247. int i;
  248. gig_dbg(DEBUG_ANY, "blocking all channels");
  249. spin_lock_irqsave(&cs->lock, flags);
  250. for (i = 0; i < cs->channels; ++i)
  251. ++cs->bcs[i].use_count;
  252. spin_unlock_irqrestore(&cs->lock, flags);
  253. }
  254. static void clear_events(struct cardstate *cs)
  255. {
  256. struct event_t *ev;
  257. unsigned head, tail;
  258. unsigned long flags;
  259. spin_lock_irqsave(&cs->ev_lock, flags);
  260. head = cs->ev_head;
  261. tail = cs->ev_tail;
  262. while (tail != head) {
  263. ev = cs->events + head;
  264. kfree(ev->ptr);
  265. head = (head + 1) % MAX_EVENTS;
  266. }
  267. cs->ev_head = tail;
  268. spin_unlock_irqrestore(&cs->ev_lock, flags);
  269. }
  270. /**
  271. * gigaset_add_event() - add event to device event queue
  272. * @cs: device descriptor structure.
  273. * @at_state: connection state structure.
  274. * @type: event type.
  275. * @ptr: pointer parameter for event.
  276. * @parameter: integer parameter for event.
  277. * @arg: pointer parameter for event.
  278. *
  279. * Allocate an event queue entry from the device's event queue, and set it up
  280. * with the parameters given.
  281. *
  282. * Return value: added event
  283. */
  284. struct event_t *gigaset_add_event(struct cardstate *cs,
  285. struct at_state_t *at_state, int type,
  286. void *ptr, int parameter, void *arg)
  287. {
  288. unsigned long flags;
  289. unsigned next, tail;
  290. struct event_t *event = NULL;
  291. spin_lock_irqsave(&cs->ev_lock, flags);
  292. tail = cs->ev_tail;
  293. next = (tail + 1) % MAX_EVENTS;
  294. if (unlikely(next == cs->ev_head))
  295. dev_err(cs->dev, "event queue full\n");
  296. else {
  297. event = cs->events + tail;
  298. event->type = type;
  299. event->at_state = at_state;
  300. event->cid = -1;
  301. event->ptr = ptr;
  302. event->arg = arg;
  303. event->parameter = parameter;
  304. cs->ev_tail = next;
  305. }
  306. spin_unlock_irqrestore(&cs->ev_lock, flags);
  307. return event;
  308. }
  309. EXPORT_SYMBOL_GPL(gigaset_add_event);
  310. static void free_strings(struct at_state_t *at_state)
  311. {
  312. int i;
  313. for (i = 0; i < STR_NUM; ++i) {
  314. kfree(at_state->str_var[i]);
  315. at_state->str_var[i] = NULL;
  316. }
  317. }
  318. static void clear_at_state(struct at_state_t *at_state)
  319. {
  320. free_strings(at_state);
  321. }
  322. static void dealloc_at_states(struct cardstate *cs)
  323. {
  324. struct at_state_t *cur, *next;
  325. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  326. list_del(&cur->list);
  327. free_strings(cur);
  328. kfree(cur);
  329. }
  330. }
  331. static void gigaset_freebcs(struct bc_state *bcs)
  332. {
  333. int i;
  334. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  335. if (!bcs->cs->ops->freebcshw(bcs)) {
  336. gig_dbg(DEBUG_INIT, "failed");
  337. }
  338. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  339. clear_at_state(&bcs->at_state);
  340. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  341. if (bcs->skb)
  342. dev_kfree_skb(bcs->skb);
  343. for (i = 0; i < AT_NUM; ++i) {
  344. kfree(bcs->commands[i]);
  345. bcs->commands[i] = NULL;
  346. }
  347. }
  348. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  349. {
  350. unsigned long flags;
  351. unsigned i;
  352. struct cardstate *cs;
  353. struct cardstate *ret = NULL;
  354. spin_lock_irqsave(&drv->lock, flags);
  355. if (drv->blocked)
  356. goto exit;
  357. for (i = 0; i < drv->minors; ++i) {
  358. cs = drv->cs + i;
  359. if (!(cs->flags & VALID_MINOR)) {
  360. cs->flags = VALID_MINOR;
  361. ret = cs;
  362. break;
  363. }
  364. }
  365. exit:
  366. spin_unlock_irqrestore(&drv->lock, flags);
  367. return ret;
  368. }
  369. static void free_cs(struct cardstate *cs)
  370. {
  371. cs->flags = 0;
  372. }
  373. static void make_valid(struct cardstate *cs, unsigned mask)
  374. {
  375. unsigned long flags;
  376. struct gigaset_driver *drv = cs->driver;
  377. spin_lock_irqsave(&drv->lock, flags);
  378. cs->flags |= mask;
  379. spin_unlock_irqrestore(&drv->lock, flags);
  380. }
  381. static void make_invalid(struct cardstate *cs, unsigned mask)
  382. {
  383. unsigned long flags;
  384. struct gigaset_driver *drv = cs->driver;
  385. spin_lock_irqsave(&drv->lock, flags);
  386. cs->flags &= ~mask;
  387. spin_unlock_irqrestore(&drv->lock, flags);
  388. }
  389. /**
  390. * gigaset_freecs() - free all associated ressources of a device
  391. * @cs: device descriptor structure.
  392. *
  393. * Stops all tasklets and timers, unregisters the device from all
  394. * subsystems it was registered to, deallocates the device structure
  395. * @cs and all structures referenced from it.
  396. * Operations on the device should be stopped before calling this.
  397. */
  398. void gigaset_freecs(struct cardstate *cs)
  399. {
  400. int i;
  401. unsigned long flags;
  402. if (!cs)
  403. return;
  404. mutex_lock(&cs->mutex);
  405. if (!cs->bcs)
  406. goto f_cs;
  407. if (!cs->inbuf)
  408. goto f_bcs;
  409. spin_lock_irqsave(&cs->lock, flags);
  410. cs->running = 0;
  411. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  412. not rescheduled below */
  413. tasklet_kill(&cs->event_tasklet);
  414. del_timer_sync(&cs->timer);
  415. switch (cs->cs_init) {
  416. default:
  417. /* clear B channel structures */
  418. for (i = 0; i < cs->channels; ++i) {
  419. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  420. gigaset_freebcs(cs->bcs + i);
  421. }
  422. /* clear device sysfs */
  423. gigaset_free_dev_sysfs(cs);
  424. gigaset_if_free(cs);
  425. gig_dbg(DEBUG_INIT, "clearing hw");
  426. cs->ops->freecshw(cs);
  427. //FIXME cmdbuf
  428. /* fall through */
  429. case 2: /* error in initcshw */
  430. /* Deregister from LL */
  431. make_invalid(cs, VALID_ID);
  432. gigaset_isdn_unregister(cs);
  433. /* fall through */
  434. case 1: /* error when registering to LL */
  435. gig_dbg(DEBUG_INIT, "clearing at_state");
  436. clear_at_state(&cs->at_state);
  437. dealloc_at_states(cs);
  438. /* fall through */
  439. case 0: /* error in basic setup */
  440. clear_events(cs);
  441. gig_dbg(DEBUG_INIT, "freeing inbuf");
  442. kfree(cs->inbuf);
  443. }
  444. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  445. kfree(cs->bcs);
  446. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  447. mutex_unlock(&cs->mutex);
  448. free_cs(cs);
  449. }
  450. EXPORT_SYMBOL_GPL(gigaset_freecs);
  451. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  452. struct cardstate *cs, int cid)
  453. {
  454. int i;
  455. INIT_LIST_HEAD(&at_state->list);
  456. at_state->waiting = 0;
  457. at_state->getstring = 0;
  458. at_state->pending_commands = 0;
  459. at_state->timer_expires = 0;
  460. at_state->timer_active = 0;
  461. at_state->timer_index = 0;
  462. at_state->seq_index = 0;
  463. at_state->ConState = 0;
  464. for (i = 0; i < STR_NUM; ++i)
  465. at_state->str_var[i] = NULL;
  466. at_state->int_var[VAR_ZDLE] = 0;
  467. at_state->int_var[VAR_ZCTP] = -1;
  468. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  469. at_state->cs = cs;
  470. at_state->bcs = bcs;
  471. at_state->cid = cid;
  472. if (!cid)
  473. at_state->replystruct = cs->tabnocid;
  474. else
  475. at_state->replystruct = cs->tabcid;
  476. }
  477. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  478. struct cardstate *cs, int inputstate)
  479. /* inbuf->read must be allocated before! */
  480. {
  481. inbuf->head = 0;
  482. inbuf->tail = 0;
  483. inbuf->cs = cs;
  484. inbuf->bcs = bcs; /*base driver: NULL*/
  485. inbuf->rcvbuf = NULL;
  486. inbuf->inputstate = inputstate;
  487. }
  488. /**
  489. * gigaset_fill_inbuf() - append received data to input buffer
  490. * @inbuf: buffer structure.
  491. * @src: received data.
  492. * @numbytes: number of bytes received.
  493. */
  494. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  495. unsigned numbytes)
  496. {
  497. unsigned n, head, tail, bytesleft;
  498. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  499. if (!numbytes)
  500. return 0;
  501. bytesleft = numbytes;
  502. tail = inbuf->tail;
  503. head = inbuf->head;
  504. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  505. while (bytesleft) {
  506. if (head > tail)
  507. n = head - 1 - tail;
  508. else if (head == 0)
  509. n = (RBUFSIZE-1) - tail;
  510. else
  511. n = RBUFSIZE - tail;
  512. if (!n) {
  513. dev_err(inbuf->cs->dev,
  514. "buffer overflow (%u bytes lost)\n",
  515. bytesleft);
  516. break;
  517. }
  518. if (n > bytesleft)
  519. n = bytesleft;
  520. memcpy(inbuf->data + tail, src, n);
  521. bytesleft -= n;
  522. tail = (tail + n) % RBUFSIZE;
  523. src += n;
  524. }
  525. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  526. inbuf->tail = tail;
  527. return numbytes != bytesleft;
  528. }
  529. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  530. /* Initialize the b-channel structure */
  531. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  532. struct cardstate *cs, int channel)
  533. {
  534. int i;
  535. bcs->tx_skb = NULL; //FIXME -> hw part
  536. skb_queue_head_init(&bcs->squeue);
  537. bcs->corrupted = 0;
  538. bcs->trans_down = 0;
  539. bcs->trans_up = 0;
  540. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  541. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  542. #ifdef CONFIG_GIGASET_DEBUG
  543. bcs->emptycount = 0;
  544. #endif
  545. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  546. bcs->fcs = PPP_INITFCS;
  547. bcs->inputstate = 0;
  548. if (cs->ignoreframes) {
  549. bcs->inputstate |= INS_skip_frame;
  550. bcs->skb = NULL;
  551. } else {
  552. bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
  553. if (bcs->skb != NULL)
  554. skb_reserve(bcs->skb, cs->hw_hdr_len);
  555. else {
  556. pr_err("out of memory\n");
  557. bcs->inputstate |= INS_skip_frame;
  558. }
  559. }
  560. bcs->channel = channel;
  561. bcs->cs = cs;
  562. bcs->chstate = 0;
  563. bcs->use_count = 1;
  564. bcs->busy = 0;
  565. bcs->ignore = cs->ignoreframes;
  566. for (i = 0; i < AT_NUM; ++i)
  567. bcs->commands[i] = NULL;
  568. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  569. if (cs->ops->initbcshw(bcs))
  570. return bcs;
  571. gig_dbg(DEBUG_INIT, " failed");
  572. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  573. if (bcs->skb)
  574. dev_kfree_skb(bcs->skb);
  575. return NULL;
  576. }
  577. /**
  578. * gigaset_initcs() - initialize device structure
  579. * @drv: hardware driver the device belongs to
  580. * @channels: number of B channels supported by device
  581. * @onechannel: !=0 if B channel data and AT commands share one
  582. * communication channel (M10x),
  583. * ==0 if B channels have separate communication channels (base)
  584. * @ignoreframes: number of frames to ignore after setting up B channel
  585. * @cidmode: !=0: start in CallID mode
  586. * @modulename: name of driver module for LL registration
  587. *
  588. * Allocate and initialize cardstate structure for Gigaset driver
  589. * Calls hardware dependent gigaset_initcshw() function
  590. * Calls B channel initialization function gigaset_initbcs() for each B channel
  591. *
  592. * Return value:
  593. * pointer to cardstate structure
  594. */
  595. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  596. int onechannel, int ignoreframes,
  597. int cidmode, const char *modulename)
  598. {
  599. struct cardstate *cs = NULL;
  600. unsigned long flags;
  601. int i;
  602. gig_dbg(DEBUG_INIT, "allocating cs");
  603. if (!(cs = alloc_cs(drv))) {
  604. pr_err("maximum number of devices exceeded\n");
  605. return NULL;
  606. }
  607. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  608. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  609. if (!cs->bcs) {
  610. pr_err("out of memory\n");
  611. goto error;
  612. }
  613. gig_dbg(DEBUG_INIT, "allocating inbuf");
  614. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  615. if (!cs->inbuf) {
  616. pr_err("out of memory\n");
  617. goto error;
  618. }
  619. cs->cs_init = 0;
  620. cs->channels = channels;
  621. cs->onechannel = onechannel;
  622. cs->ignoreframes = ignoreframes;
  623. INIT_LIST_HEAD(&cs->temp_at_states);
  624. cs->running = 0;
  625. init_timer(&cs->timer); /* clear next & prev */
  626. spin_lock_init(&cs->ev_lock);
  627. cs->ev_tail = 0;
  628. cs->ev_head = 0;
  629. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  630. (unsigned long) cs);
  631. cs->commands_pending = 0;
  632. cs->cur_at_seq = 0;
  633. cs->gotfwver = -1;
  634. cs->open_count = 0;
  635. cs->dev = NULL;
  636. cs->tty = NULL;
  637. cs->tty_dev = NULL;
  638. cs->cidmode = cidmode != 0;
  639. cs->tabnocid = gigaset_tab_nocid;
  640. cs->tabcid = gigaset_tab_cid;
  641. init_waitqueue_head(&cs->waitqueue);
  642. cs->waiting = 0;
  643. cs->mode = M_UNKNOWN;
  644. cs->mstate = MS_UNINITIALIZED;
  645. ++cs->cs_init;
  646. gig_dbg(DEBUG_INIT, "setting up at_state");
  647. spin_lock_init(&cs->lock);
  648. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  649. cs->dle = 0;
  650. cs->cbytes = 0;
  651. gig_dbg(DEBUG_INIT, "setting up inbuf");
  652. if (onechannel) { //FIXME distinction necessary?
  653. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  654. } else
  655. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  656. cs->connected = 0;
  657. cs->isdn_up = 0;
  658. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  659. cs->cmdbuf = cs->lastcmdbuf = NULL;
  660. spin_lock_init(&cs->cmdlock);
  661. cs->curlen = 0;
  662. cs->cmdbytes = 0;
  663. gig_dbg(DEBUG_INIT, "setting up iif");
  664. if (!gigaset_isdn_register(cs, modulename)) {
  665. pr_err("error registering ISDN device\n");
  666. goto error;
  667. }
  668. make_valid(cs, VALID_ID);
  669. ++cs->cs_init;
  670. gig_dbg(DEBUG_INIT, "setting up hw");
  671. if (!cs->ops->initcshw(cs))
  672. goto error;
  673. ++cs->cs_init;
  674. /* set up character device */
  675. gigaset_if_init(cs);
  676. /* set up device sysfs */
  677. gigaset_init_dev_sysfs(cs);
  678. /* set up channel data structures */
  679. for (i = 0; i < channels; ++i) {
  680. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  681. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  682. pr_err("could not allocate channel %d data\n", i);
  683. goto error;
  684. }
  685. }
  686. spin_lock_irqsave(&cs->lock, flags);
  687. cs->running = 1;
  688. spin_unlock_irqrestore(&cs->lock, flags);
  689. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  690. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  691. /* FIXME: can jiffies increase too much until the timer is added?
  692. * Same problem(?) with mod_timer() in timer_tick(). */
  693. add_timer(&cs->timer);
  694. gig_dbg(DEBUG_INIT, "cs initialized");
  695. return cs;
  696. error:
  697. gig_dbg(DEBUG_INIT, "failed");
  698. gigaset_freecs(cs);
  699. return NULL;
  700. }
  701. EXPORT_SYMBOL_GPL(gigaset_initcs);
  702. /* ReInitialize the b-channel structure on hangup */
  703. void gigaset_bcs_reinit(struct bc_state *bcs)
  704. {
  705. struct sk_buff *skb;
  706. struct cardstate *cs = bcs->cs;
  707. unsigned long flags;
  708. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  709. dev_kfree_skb(skb);
  710. spin_lock_irqsave(&cs->lock, flags);
  711. clear_at_state(&bcs->at_state);
  712. bcs->at_state.ConState = 0;
  713. bcs->at_state.timer_active = 0;
  714. bcs->at_state.timer_expires = 0;
  715. bcs->at_state.cid = -1; /* No CID defined */
  716. spin_unlock_irqrestore(&cs->lock, flags);
  717. bcs->inputstate = 0;
  718. #ifdef CONFIG_GIGASET_DEBUG
  719. bcs->emptycount = 0;
  720. #endif
  721. bcs->fcs = PPP_INITFCS;
  722. bcs->chstate = 0;
  723. bcs->ignore = cs->ignoreframes;
  724. if (bcs->ignore)
  725. bcs->inputstate |= INS_skip_frame;
  726. cs->ops->reinitbcshw(bcs);
  727. }
  728. static void cleanup_cs(struct cardstate *cs)
  729. {
  730. struct cmdbuf_t *cb, *tcb;
  731. int i;
  732. unsigned long flags;
  733. spin_lock_irqsave(&cs->lock, flags);
  734. cs->mode = M_UNKNOWN;
  735. cs->mstate = MS_UNINITIALIZED;
  736. clear_at_state(&cs->at_state);
  737. dealloc_at_states(cs);
  738. free_strings(&cs->at_state);
  739. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  740. kfree(cs->inbuf->rcvbuf);
  741. cs->inbuf->rcvbuf = NULL;
  742. cs->inbuf->inputstate = INS_command;
  743. cs->inbuf->head = 0;
  744. cs->inbuf->tail = 0;
  745. cb = cs->cmdbuf;
  746. while (cb) {
  747. tcb = cb;
  748. cb = cb->next;
  749. kfree(tcb);
  750. }
  751. cs->cmdbuf = cs->lastcmdbuf = NULL;
  752. cs->curlen = 0;
  753. cs->cmdbytes = 0;
  754. cs->gotfwver = -1;
  755. cs->dle = 0;
  756. cs->cur_at_seq = 0;
  757. cs->commands_pending = 0;
  758. cs->cbytes = 0;
  759. spin_unlock_irqrestore(&cs->lock, flags);
  760. for (i = 0; i < cs->channels; ++i) {
  761. gigaset_freebcs(cs->bcs + i);
  762. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  763. pr_err("could not allocate channel %d data\n", i);
  764. }
  765. if (cs->waiting) {
  766. cs->cmd_result = -ENODEV;
  767. cs->waiting = 0;
  768. wake_up_interruptible(&cs->waitqueue);
  769. }
  770. }
  771. /**
  772. * gigaset_start() - start device operations
  773. * @cs: device descriptor structure.
  774. *
  775. * Prepares the device for use by setting up communication parameters,
  776. * scheduling an EV_START event to initiate device initialization, and
  777. * waiting for completion of the initialization.
  778. *
  779. * Return value:
  780. * 1 - success, 0 - error
  781. */
  782. int gigaset_start(struct cardstate *cs)
  783. {
  784. unsigned long flags;
  785. if (mutex_lock_interruptible(&cs->mutex))
  786. return 0;
  787. spin_lock_irqsave(&cs->lock, flags);
  788. cs->connected = 1;
  789. spin_unlock_irqrestore(&cs->lock, flags);
  790. if (cs->mstate != MS_LOCKED) {
  791. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  792. cs->ops->baud_rate(cs, B115200);
  793. cs->ops->set_line_ctrl(cs, CS8);
  794. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  795. } else {
  796. //FIXME use some saved values?
  797. }
  798. cs->waiting = 1;
  799. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  800. cs->waiting = 0;
  801. //FIXME what should we do?
  802. goto error;
  803. }
  804. gig_dbg(DEBUG_CMD, "scheduling START");
  805. gigaset_schedule_event(cs);
  806. wait_event(cs->waitqueue, !cs->waiting);
  807. mutex_unlock(&cs->mutex);
  808. return 1;
  809. error:
  810. mutex_unlock(&cs->mutex);
  811. return 0;
  812. }
  813. EXPORT_SYMBOL_GPL(gigaset_start);
  814. /**
  815. * gigaset_shutdown() - shut down device operations
  816. * @cs: device descriptor structure.
  817. *
  818. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  819. * waiting for completion of the shutdown.
  820. *
  821. * Return value:
  822. * 0 - success, -1 - error (no device associated)
  823. */
  824. int gigaset_shutdown(struct cardstate *cs)
  825. {
  826. mutex_lock(&cs->mutex);
  827. if (!(cs->flags & VALID_MINOR)) {
  828. mutex_unlock(&cs->mutex);
  829. return -1;
  830. }
  831. cs->waiting = 1;
  832. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  833. //FIXME what should we do?
  834. goto exit;
  835. }
  836. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  837. gigaset_schedule_event(cs);
  838. wait_event(cs->waitqueue, !cs->waiting);
  839. cleanup_cs(cs);
  840. exit:
  841. mutex_unlock(&cs->mutex);
  842. return 0;
  843. }
  844. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  845. /**
  846. * gigaset_stop() - stop device operations
  847. * @cs: device descriptor structure.
  848. *
  849. * Stops operations on the device by scheduling an EV_STOP event and
  850. * waiting for completion of the shutdown.
  851. */
  852. void gigaset_stop(struct cardstate *cs)
  853. {
  854. mutex_lock(&cs->mutex);
  855. cs->waiting = 1;
  856. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  857. //FIXME what should we do?
  858. goto exit;
  859. }
  860. gig_dbg(DEBUG_CMD, "scheduling STOP");
  861. gigaset_schedule_event(cs);
  862. wait_event(cs->waitqueue, !cs->waiting);
  863. cleanup_cs(cs);
  864. exit:
  865. mutex_unlock(&cs->mutex);
  866. }
  867. EXPORT_SYMBOL_GPL(gigaset_stop);
  868. static LIST_HEAD(drivers);
  869. static DEFINE_SPINLOCK(driver_lock);
  870. struct cardstate *gigaset_get_cs_by_id(int id)
  871. {
  872. unsigned long flags;
  873. struct cardstate *ret = NULL;
  874. struct cardstate *cs;
  875. struct gigaset_driver *drv;
  876. unsigned i;
  877. spin_lock_irqsave(&driver_lock, flags);
  878. list_for_each_entry(drv, &drivers, list) {
  879. spin_lock(&drv->lock);
  880. for (i = 0; i < drv->minors; ++i) {
  881. cs = drv->cs + i;
  882. if ((cs->flags & VALID_ID) && cs->myid == id) {
  883. ret = cs;
  884. break;
  885. }
  886. }
  887. spin_unlock(&drv->lock);
  888. if (ret)
  889. break;
  890. }
  891. spin_unlock_irqrestore(&driver_lock, flags);
  892. return ret;
  893. }
  894. void gigaset_debugdrivers(void)
  895. {
  896. unsigned long flags;
  897. static struct cardstate *cs;
  898. struct gigaset_driver *drv;
  899. unsigned i;
  900. spin_lock_irqsave(&driver_lock, flags);
  901. list_for_each_entry(drv, &drivers, list) {
  902. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  903. spin_lock(&drv->lock);
  904. for (i = 0; i < drv->minors; ++i) {
  905. gig_dbg(DEBUG_DRIVER, " index %u", i);
  906. cs = drv->cs + i;
  907. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  908. gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
  909. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  910. cs->minor_index);
  911. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  912. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  913. }
  914. spin_unlock(&drv->lock);
  915. }
  916. spin_unlock_irqrestore(&driver_lock, flags);
  917. }
  918. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  919. {
  920. unsigned long flags;
  921. struct cardstate *ret = NULL;
  922. struct gigaset_driver *drv;
  923. unsigned index;
  924. spin_lock_irqsave(&driver_lock, flags);
  925. list_for_each_entry(drv, &drivers, list) {
  926. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  927. continue;
  928. index = minor - drv->minor;
  929. spin_lock(&drv->lock);
  930. if (drv->cs[index].flags & VALID_MINOR)
  931. ret = drv->cs + index;
  932. spin_unlock(&drv->lock);
  933. if (ret)
  934. break;
  935. }
  936. spin_unlock_irqrestore(&driver_lock, flags);
  937. return ret;
  938. }
  939. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  940. {
  941. if (tty->index < 0 || tty->index >= tty->driver->num)
  942. return NULL;
  943. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  944. }
  945. /**
  946. * gigaset_freedriver() - free all associated ressources of a driver
  947. * @drv: driver descriptor structure.
  948. *
  949. * Unregisters the driver from the system and deallocates the driver
  950. * structure @drv and all structures referenced from it.
  951. * All devices should be shut down before calling this.
  952. */
  953. void gigaset_freedriver(struct gigaset_driver *drv)
  954. {
  955. unsigned long flags;
  956. spin_lock_irqsave(&driver_lock, flags);
  957. list_del(&drv->list);
  958. spin_unlock_irqrestore(&driver_lock, flags);
  959. gigaset_if_freedriver(drv);
  960. kfree(drv->cs);
  961. kfree(drv);
  962. }
  963. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  964. /**
  965. * gigaset_initdriver() - initialize driver structure
  966. * @minor: First minor number
  967. * @minors: Number of minors this driver can handle
  968. * @procname: Name of the driver
  969. * @devname: Name of the device files (prefix without minor number)
  970. *
  971. * Allocate and initialize gigaset_driver structure. Initialize interface.
  972. *
  973. * Return value:
  974. * Pointer to the gigaset_driver structure on success, NULL on failure.
  975. */
  976. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  977. const char *procname,
  978. const char *devname,
  979. const struct gigaset_ops *ops,
  980. struct module *owner)
  981. {
  982. struct gigaset_driver *drv;
  983. unsigned long flags;
  984. unsigned i;
  985. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  986. if (!drv)
  987. return NULL;
  988. drv->have_tty = 0;
  989. drv->minor = minor;
  990. drv->minors = minors;
  991. spin_lock_init(&drv->lock);
  992. drv->blocked = 0;
  993. drv->ops = ops;
  994. drv->owner = owner;
  995. INIT_LIST_HEAD(&drv->list);
  996. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  997. if (!drv->cs)
  998. goto error;
  999. for (i = 0; i < minors; ++i) {
  1000. drv->cs[i].flags = 0;
  1001. drv->cs[i].driver = drv;
  1002. drv->cs[i].ops = drv->ops;
  1003. drv->cs[i].minor_index = i;
  1004. mutex_init(&drv->cs[i].mutex);
  1005. }
  1006. gigaset_if_initdriver(drv, procname, devname);
  1007. spin_lock_irqsave(&driver_lock, flags);
  1008. list_add(&drv->list, &drivers);
  1009. spin_unlock_irqrestore(&driver_lock, flags);
  1010. return drv;
  1011. error:
  1012. kfree(drv->cs);
  1013. kfree(drv);
  1014. return NULL;
  1015. }
  1016. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  1017. /**
  1018. * gigaset_blockdriver() - block driver
  1019. * @drv: driver descriptor structure.
  1020. *
  1021. * Prevents the driver from attaching new devices, in preparation for
  1022. * deregistration.
  1023. */
  1024. void gigaset_blockdriver(struct gigaset_driver *drv)
  1025. {
  1026. drv->blocked = 1;
  1027. }
  1028. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  1029. static int __init gigaset_init_module(void)
  1030. {
  1031. /* in accordance with the principle of least astonishment,
  1032. * setting the 'debug' parameter to 1 activates a sensible
  1033. * set of default debug levels
  1034. */
  1035. if (gigaset_debuglevel == 1)
  1036. gigaset_debuglevel = DEBUG_DEFAULT;
  1037. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  1038. return 0;
  1039. }
  1040. static void __exit gigaset_exit_module(void)
  1041. {
  1042. }
  1043. module_init(gigaset_init_module);
  1044. module_exit(gigaset_exit_module);
  1045. MODULE_AUTHOR(DRIVER_AUTHOR);
  1046. MODULE_DESCRIPTION(DRIVER_DESC);
  1047. MODULE_LICENSE("GPL");