common.c 26 KB

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