common.c 25 KB

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