common.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 0;
  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 1;
  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 0;
  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 1;
  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 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. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  338. clear_at_state(&bcs->at_state);
  339. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  340. dev_kfree_skb(bcs->rx_skb);
  341. bcs->rx_skb = NULL;
  342. for (i = 0; i < AT_NUM; ++i) {
  343. kfree(bcs->commands[i]);
  344. bcs->commands[i] = NULL;
  345. }
  346. }
  347. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  348. {
  349. unsigned long flags;
  350. unsigned i;
  351. struct cardstate *cs;
  352. struct cardstate *ret = NULL;
  353. spin_lock_irqsave(&drv->lock, flags);
  354. if (drv->blocked)
  355. goto exit;
  356. for (i = 0; i < drv->minors; ++i) {
  357. cs = drv->cs + i;
  358. if (!(cs->flags & VALID_MINOR)) {
  359. cs->flags = VALID_MINOR;
  360. ret = cs;
  361. break;
  362. }
  363. }
  364. exit:
  365. spin_unlock_irqrestore(&drv->lock, flags);
  366. return ret;
  367. }
  368. static void free_cs(struct cardstate *cs)
  369. {
  370. cs->flags = 0;
  371. }
  372. static void make_valid(struct cardstate *cs, unsigned mask)
  373. {
  374. unsigned long flags;
  375. struct gigaset_driver *drv = cs->driver;
  376. spin_lock_irqsave(&drv->lock, flags);
  377. cs->flags |= mask;
  378. spin_unlock_irqrestore(&drv->lock, flags);
  379. }
  380. static void make_invalid(struct cardstate *cs, unsigned mask)
  381. {
  382. unsigned long flags;
  383. struct gigaset_driver *drv = cs->driver;
  384. spin_lock_irqsave(&drv->lock, flags);
  385. cs->flags &= ~mask;
  386. spin_unlock_irqrestore(&drv->lock, flags);
  387. }
  388. /**
  389. * gigaset_freecs() - free all associated ressources of a device
  390. * @cs: device descriptor structure.
  391. *
  392. * Stops all tasklets and timers, unregisters the device from all
  393. * subsystems it was registered to, deallocates the device structure
  394. * @cs and all structures referenced from it.
  395. * Operations on the device should be stopped before calling this.
  396. */
  397. void gigaset_freecs(struct cardstate *cs)
  398. {
  399. int i;
  400. unsigned long flags;
  401. if (!cs)
  402. return;
  403. mutex_lock(&cs->mutex);
  404. if (!cs->bcs)
  405. goto f_cs;
  406. if (!cs->inbuf)
  407. goto f_bcs;
  408. spin_lock_irqsave(&cs->lock, flags);
  409. cs->running = 0;
  410. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  411. not rescheduled below */
  412. tasklet_kill(&cs->event_tasklet);
  413. del_timer_sync(&cs->timer);
  414. switch (cs->cs_init) {
  415. default:
  416. /* clear B channel structures */
  417. for (i = 0; i < cs->channels; ++i) {
  418. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  419. gigaset_freebcs(cs->bcs + i);
  420. }
  421. /* clear device sysfs */
  422. gigaset_free_dev_sysfs(cs);
  423. gigaset_if_free(cs);
  424. gig_dbg(DEBUG_INIT, "clearing hw");
  425. cs->ops->freecshw(cs);
  426. /* fall through */
  427. case 2: /* error in initcshw */
  428. /* Deregister from LL */
  429. make_invalid(cs, VALID_ID);
  430. gigaset_isdn_unregdev(cs);
  431. /* fall through */
  432. case 1: /* error when registering to LL */
  433. gig_dbg(DEBUG_INIT, "clearing at_state");
  434. clear_at_state(&cs->at_state);
  435. dealloc_at_states(cs);
  436. /* fall through */
  437. case 0: /* error in basic setup */
  438. clear_events(cs);
  439. gig_dbg(DEBUG_INIT, "freeing inbuf");
  440. kfree(cs->inbuf);
  441. }
  442. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  443. kfree(cs->bcs);
  444. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  445. mutex_unlock(&cs->mutex);
  446. free_cs(cs);
  447. }
  448. EXPORT_SYMBOL_GPL(gigaset_freecs);
  449. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  450. struct cardstate *cs, int cid)
  451. {
  452. int i;
  453. INIT_LIST_HEAD(&at_state->list);
  454. at_state->waiting = 0;
  455. at_state->getstring = 0;
  456. at_state->pending_commands = 0;
  457. at_state->timer_expires = 0;
  458. at_state->timer_active = 0;
  459. at_state->timer_index = 0;
  460. at_state->seq_index = 0;
  461. at_state->ConState = 0;
  462. for (i = 0; i < STR_NUM; ++i)
  463. at_state->str_var[i] = NULL;
  464. at_state->int_var[VAR_ZDLE] = 0;
  465. at_state->int_var[VAR_ZCTP] = -1;
  466. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  467. at_state->cs = cs;
  468. at_state->bcs = bcs;
  469. at_state->cid = cid;
  470. if (!cid)
  471. at_state->replystruct = cs->tabnocid;
  472. else
  473. at_state->replystruct = cs->tabcid;
  474. }
  475. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
  476. /* inbuf->read must be allocated before! */
  477. {
  478. inbuf->head = 0;
  479. inbuf->tail = 0;
  480. inbuf->cs = cs;
  481. inbuf->inputstate = INS_command;
  482. }
  483. /**
  484. * gigaset_fill_inbuf() - append received data to input buffer
  485. * @inbuf: buffer structure.
  486. * @src: received data.
  487. * @numbytes: number of bytes received.
  488. */
  489. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  490. unsigned numbytes)
  491. {
  492. unsigned n, head, tail, bytesleft;
  493. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  494. if (!numbytes)
  495. return 0;
  496. bytesleft = numbytes;
  497. tail = inbuf->tail;
  498. head = inbuf->head;
  499. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  500. while (bytesleft) {
  501. if (head > tail)
  502. n = head - 1 - tail;
  503. else if (head == 0)
  504. n = (RBUFSIZE-1) - tail;
  505. else
  506. n = RBUFSIZE - tail;
  507. if (!n) {
  508. dev_err(inbuf->cs->dev,
  509. "buffer overflow (%u bytes lost)\n",
  510. bytesleft);
  511. break;
  512. }
  513. if (n > bytesleft)
  514. n = bytesleft;
  515. memcpy(inbuf->data + tail, src, n);
  516. bytesleft -= n;
  517. tail = (tail + n) % RBUFSIZE;
  518. src += n;
  519. }
  520. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  521. inbuf->tail = tail;
  522. return numbytes != bytesleft;
  523. }
  524. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  525. /* Initialize the b-channel structure */
  526. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  527. struct cardstate *cs, int channel)
  528. {
  529. int i;
  530. bcs->tx_skb = NULL;
  531. skb_queue_head_init(&bcs->squeue);
  532. bcs->corrupted = 0;
  533. bcs->trans_down = 0;
  534. bcs->trans_up = 0;
  535. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  536. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  537. #ifdef CONFIG_GIGASET_DEBUG
  538. bcs->emptycount = 0;
  539. #endif
  540. bcs->rx_bufsize = 0;
  541. bcs->rx_skb = NULL;
  542. bcs->rx_fcs = PPP_INITFCS;
  543. bcs->inputstate = 0;
  544. bcs->channel = channel;
  545. bcs->cs = cs;
  546. bcs->chstate = 0;
  547. bcs->use_count = 1;
  548. bcs->busy = 0;
  549. bcs->ignore = cs->ignoreframes;
  550. for (i = 0; i < AT_NUM; ++i)
  551. bcs->commands[i] = NULL;
  552. spin_lock_init(&bcs->aplock);
  553. bcs->ap = NULL;
  554. bcs->apconnstate = 0;
  555. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  556. if (cs->ops->initbcshw(bcs))
  557. return bcs;
  558. gig_dbg(DEBUG_INIT, " failed");
  559. return NULL;
  560. }
  561. /**
  562. * gigaset_initcs() - initialize device structure
  563. * @drv: hardware driver the device belongs to
  564. * @channels: number of B channels supported by device
  565. * @onechannel: !=0 if B channel data and AT commands share one
  566. * communication channel (M10x),
  567. * ==0 if B channels have separate communication channels (base)
  568. * @ignoreframes: number of frames to ignore after setting up B channel
  569. * @cidmode: !=0: start in CallID mode
  570. * @modulename: name of driver module for LL registration
  571. *
  572. * Allocate and initialize cardstate structure for Gigaset driver
  573. * Calls hardware dependent gigaset_initcshw() function
  574. * Calls B channel initialization function gigaset_initbcs() for each B channel
  575. *
  576. * Return value:
  577. * pointer to cardstate structure
  578. */
  579. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  580. int onechannel, int ignoreframes,
  581. int cidmode, const char *modulename)
  582. {
  583. struct cardstate *cs;
  584. unsigned long flags;
  585. int i;
  586. gig_dbg(DEBUG_INIT, "allocating cs");
  587. cs = alloc_cs(drv);
  588. if (!cs) {
  589. pr_err("maximum number of devices exceeded\n");
  590. return NULL;
  591. }
  592. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  593. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  594. if (!cs->bcs) {
  595. pr_err("out of memory\n");
  596. goto error;
  597. }
  598. gig_dbg(DEBUG_INIT, "allocating inbuf");
  599. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  600. if (!cs->inbuf) {
  601. pr_err("out of memory\n");
  602. goto error;
  603. }
  604. cs->cs_init = 0;
  605. cs->channels = channels;
  606. cs->onechannel = onechannel;
  607. cs->ignoreframes = ignoreframes;
  608. INIT_LIST_HEAD(&cs->temp_at_states);
  609. cs->running = 0;
  610. init_timer(&cs->timer); /* clear next & prev */
  611. spin_lock_init(&cs->ev_lock);
  612. cs->ev_tail = 0;
  613. cs->ev_head = 0;
  614. tasklet_init(&cs->event_tasklet, gigaset_handle_event,
  615. (unsigned long) cs);
  616. cs->commands_pending = 0;
  617. cs->cur_at_seq = 0;
  618. cs->gotfwver = -1;
  619. cs->open_count = 0;
  620. cs->dev = NULL;
  621. cs->tty = NULL;
  622. cs->tty_dev = NULL;
  623. cs->cidmode = cidmode != 0;
  624. cs->tabnocid = gigaset_tab_nocid;
  625. cs->tabcid = gigaset_tab_cid;
  626. init_waitqueue_head(&cs->waitqueue);
  627. cs->waiting = 0;
  628. cs->mode = M_UNKNOWN;
  629. cs->mstate = MS_UNINITIALIZED;
  630. ++cs->cs_init;
  631. gig_dbg(DEBUG_INIT, "setting up at_state");
  632. spin_lock_init(&cs->lock);
  633. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  634. cs->dle = 0;
  635. cs->cbytes = 0;
  636. gig_dbg(DEBUG_INIT, "setting up inbuf");
  637. gigaset_inbuf_init(cs->inbuf, cs);
  638. cs->connected = 0;
  639. cs->isdn_up = 0;
  640. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  641. cs->cmdbuf = cs->lastcmdbuf = NULL;
  642. spin_lock_init(&cs->cmdlock);
  643. cs->curlen = 0;
  644. cs->cmdbytes = 0;
  645. gig_dbg(DEBUG_INIT, "setting up iif");
  646. if (!gigaset_isdn_regdev(cs, modulename)) {
  647. pr_err("error registering ISDN device\n");
  648. goto error;
  649. }
  650. make_valid(cs, VALID_ID);
  651. ++cs->cs_init;
  652. gig_dbg(DEBUG_INIT, "setting up hw");
  653. if (!cs->ops->initcshw(cs))
  654. goto error;
  655. ++cs->cs_init;
  656. /* set up character device */
  657. gigaset_if_init(cs);
  658. /* set up device sysfs */
  659. gigaset_init_dev_sysfs(cs);
  660. /* set up channel data structures */
  661. for (i = 0; i < channels; ++i) {
  662. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  663. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  664. pr_err("could not allocate channel %d data\n", i);
  665. goto error;
  666. }
  667. }
  668. spin_lock_irqsave(&cs->lock, flags);
  669. cs->running = 1;
  670. spin_unlock_irqrestore(&cs->lock, flags);
  671. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  672. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  673. add_timer(&cs->timer);
  674. gig_dbg(DEBUG_INIT, "cs initialized");
  675. return cs;
  676. error:
  677. gig_dbg(DEBUG_INIT, "failed");
  678. gigaset_freecs(cs);
  679. return NULL;
  680. }
  681. EXPORT_SYMBOL_GPL(gigaset_initcs);
  682. /* ReInitialize the b-channel structure on hangup */
  683. void gigaset_bcs_reinit(struct bc_state *bcs)
  684. {
  685. struct sk_buff *skb;
  686. struct cardstate *cs = bcs->cs;
  687. unsigned long flags;
  688. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  689. dev_kfree_skb(skb);
  690. spin_lock_irqsave(&cs->lock, flags);
  691. clear_at_state(&bcs->at_state);
  692. bcs->at_state.ConState = 0;
  693. bcs->at_state.timer_active = 0;
  694. bcs->at_state.timer_expires = 0;
  695. bcs->at_state.cid = -1; /* No CID defined */
  696. spin_unlock_irqrestore(&cs->lock, flags);
  697. bcs->inputstate = 0;
  698. #ifdef CONFIG_GIGASET_DEBUG
  699. bcs->emptycount = 0;
  700. #endif
  701. bcs->rx_fcs = PPP_INITFCS;
  702. bcs->chstate = 0;
  703. bcs->ignore = cs->ignoreframes;
  704. dev_kfree_skb(bcs->rx_skb);
  705. bcs->rx_skb = NULL;
  706. cs->ops->reinitbcshw(bcs);
  707. }
  708. static void cleanup_cs(struct cardstate *cs)
  709. {
  710. struct cmdbuf_t *cb, *tcb;
  711. int i;
  712. unsigned long flags;
  713. spin_lock_irqsave(&cs->lock, flags);
  714. cs->mode = M_UNKNOWN;
  715. cs->mstate = MS_UNINITIALIZED;
  716. clear_at_state(&cs->at_state);
  717. dealloc_at_states(cs);
  718. free_strings(&cs->at_state);
  719. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  720. cs->inbuf->inputstate = INS_command;
  721. cs->inbuf->head = 0;
  722. cs->inbuf->tail = 0;
  723. cb = cs->cmdbuf;
  724. while (cb) {
  725. tcb = cb;
  726. cb = cb->next;
  727. kfree(tcb);
  728. }
  729. cs->cmdbuf = cs->lastcmdbuf = NULL;
  730. cs->curlen = 0;
  731. cs->cmdbytes = 0;
  732. cs->gotfwver = -1;
  733. cs->dle = 0;
  734. cs->cur_at_seq = 0;
  735. cs->commands_pending = 0;
  736. cs->cbytes = 0;
  737. spin_unlock_irqrestore(&cs->lock, flags);
  738. for (i = 0; i < cs->channels; ++i) {
  739. gigaset_freebcs(cs->bcs + i);
  740. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  741. pr_err("could not allocate channel %d data\n", i);
  742. }
  743. if (cs->waiting) {
  744. cs->cmd_result = -ENODEV;
  745. cs->waiting = 0;
  746. wake_up_interruptible(&cs->waitqueue);
  747. }
  748. }
  749. /**
  750. * gigaset_start() - start device operations
  751. * @cs: device descriptor structure.
  752. *
  753. * Prepares the device for use by setting up communication parameters,
  754. * scheduling an EV_START event to initiate device initialization, and
  755. * waiting for completion of the initialization.
  756. *
  757. * Return value:
  758. * 1 - success, 0 - error
  759. */
  760. int gigaset_start(struct cardstate *cs)
  761. {
  762. unsigned long flags;
  763. if (mutex_lock_interruptible(&cs->mutex))
  764. return 0;
  765. spin_lock_irqsave(&cs->lock, flags);
  766. cs->connected = 1;
  767. spin_unlock_irqrestore(&cs->lock, flags);
  768. if (cs->mstate != MS_LOCKED) {
  769. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  770. cs->ops->baud_rate(cs, B115200);
  771. cs->ops->set_line_ctrl(cs, CS8);
  772. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  773. }
  774. cs->waiting = 1;
  775. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  776. cs->waiting = 0;
  777. goto error;
  778. }
  779. gigaset_schedule_event(cs);
  780. wait_event(cs->waitqueue, !cs->waiting);
  781. mutex_unlock(&cs->mutex);
  782. return 1;
  783. error:
  784. mutex_unlock(&cs->mutex);
  785. return 0;
  786. }
  787. EXPORT_SYMBOL_GPL(gigaset_start);
  788. /**
  789. * gigaset_shutdown() - shut down device operations
  790. * @cs: device descriptor structure.
  791. *
  792. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  793. * waiting for completion of the shutdown.
  794. *
  795. * Return value:
  796. * 0 - success, -1 - error (no device associated)
  797. */
  798. int gigaset_shutdown(struct cardstate *cs)
  799. {
  800. mutex_lock(&cs->mutex);
  801. if (!(cs->flags & VALID_MINOR)) {
  802. mutex_unlock(&cs->mutex);
  803. return -1;
  804. }
  805. cs->waiting = 1;
  806. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
  807. goto exit;
  808. gigaset_schedule_event(cs);
  809. wait_event(cs->waitqueue, !cs->waiting);
  810. cleanup_cs(cs);
  811. exit:
  812. mutex_unlock(&cs->mutex);
  813. return 0;
  814. }
  815. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  816. /**
  817. * gigaset_stop() - stop device operations
  818. * @cs: device descriptor structure.
  819. *
  820. * Stops operations on the device by scheduling an EV_STOP event and
  821. * waiting for completion of the shutdown.
  822. */
  823. void gigaset_stop(struct cardstate *cs)
  824. {
  825. mutex_lock(&cs->mutex);
  826. cs->waiting = 1;
  827. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
  828. goto exit;
  829. gigaset_schedule_event(cs);
  830. wait_event(cs->waitqueue, !cs->waiting);
  831. cleanup_cs(cs);
  832. exit:
  833. mutex_unlock(&cs->mutex);
  834. }
  835. EXPORT_SYMBOL_GPL(gigaset_stop);
  836. static LIST_HEAD(drivers);
  837. static DEFINE_SPINLOCK(driver_lock);
  838. struct cardstate *gigaset_get_cs_by_id(int id)
  839. {
  840. unsigned long flags;
  841. struct cardstate *ret = NULL;
  842. struct cardstate *cs;
  843. struct gigaset_driver *drv;
  844. unsigned i;
  845. spin_lock_irqsave(&driver_lock, flags);
  846. list_for_each_entry(drv, &drivers, list) {
  847. spin_lock(&drv->lock);
  848. for (i = 0; i < drv->minors; ++i) {
  849. cs = drv->cs + i;
  850. if ((cs->flags & VALID_ID) && cs->myid == id) {
  851. ret = cs;
  852. break;
  853. }
  854. }
  855. spin_unlock(&drv->lock);
  856. if (ret)
  857. break;
  858. }
  859. spin_unlock_irqrestore(&driver_lock, flags);
  860. return ret;
  861. }
  862. void gigaset_debugdrivers(void)
  863. {
  864. unsigned long flags;
  865. static struct cardstate *cs;
  866. struct gigaset_driver *drv;
  867. unsigned i;
  868. spin_lock_irqsave(&driver_lock, flags);
  869. list_for_each_entry(drv, &drivers, list) {
  870. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  871. spin_lock(&drv->lock);
  872. for (i = 0; i < drv->minors; ++i) {
  873. gig_dbg(DEBUG_DRIVER, " index %u", i);
  874. cs = drv->cs + i;
  875. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  876. gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
  877. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  878. cs->minor_index);
  879. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  880. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  881. }
  882. spin_unlock(&drv->lock);
  883. }
  884. spin_unlock_irqrestore(&driver_lock, flags);
  885. }
  886. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  887. {
  888. unsigned long flags;
  889. struct cardstate *ret = NULL;
  890. struct gigaset_driver *drv;
  891. unsigned index;
  892. spin_lock_irqsave(&driver_lock, flags);
  893. list_for_each_entry(drv, &drivers, list) {
  894. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  895. continue;
  896. index = minor - drv->minor;
  897. spin_lock(&drv->lock);
  898. if (drv->cs[index].flags & VALID_MINOR)
  899. ret = drv->cs + index;
  900. spin_unlock(&drv->lock);
  901. if (ret)
  902. break;
  903. }
  904. spin_unlock_irqrestore(&driver_lock, flags);
  905. return ret;
  906. }
  907. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  908. {
  909. if (tty->index < 0 || tty->index >= tty->driver->num)
  910. return NULL;
  911. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  912. }
  913. /**
  914. * gigaset_freedriver() - free all associated ressources of a driver
  915. * @drv: driver descriptor structure.
  916. *
  917. * Unregisters the driver from the system and deallocates the driver
  918. * structure @drv and all structures referenced from it.
  919. * All devices should be shut down before calling this.
  920. */
  921. void gigaset_freedriver(struct gigaset_driver *drv)
  922. {
  923. unsigned long flags;
  924. spin_lock_irqsave(&driver_lock, flags);
  925. list_del(&drv->list);
  926. spin_unlock_irqrestore(&driver_lock, flags);
  927. gigaset_if_freedriver(drv);
  928. kfree(drv->cs);
  929. kfree(drv);
  930. }
  931. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  932. /**
  933. * gigaset_initdriver() - initialize driver structure
  934. * @minor: First minor number
  935. * @minors: Number of minors this driver can handle
  936. * @procname: Name of the driver
  937. * @devname: Name of the device files (prefix without minor number)
  938. *
  939. * Allocate and initialize gigaset_driver structure. Initialize interface.
  940. *
  941. * Return value:
  942. * Pointer to the gigaset_driver structure on success, NULL on failure.
  943. */
  944. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  945. const char *procname,
  946. const char *devname,
  947. const struct gigaset_ops *ops,
  948. struct module *owner)
  949. {
  950. struct gigaset_driver *drv;
  951. unsigned long flags;
  952. unsigned i;
  953. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  954. if (!drv)
  955. return NULL;
  956. drv->have_tty = 0;
  957. drv->minor = minor;
  958. drv->minors = minors;
  959. spin_lock_init(&drv->lock);
  960. drv->blocked = 0;
  961. drv->ops = ops;
  962. drv->owner = owner;
  963. INIT_LIST_HEAD(&drv->list);
  964. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  965. if (!drv->cs)
  966. goto error;
  967. for (i = 0; i < minors; ++i) {
  968. drv->cs[i].flags = 0;
  969. drv->cs[i].driver = drv;
  970. drv->cs[i].ops = drv->ops;
  971. drv->cs[i].minor_index = i;
  972. mutex_init(&drv->cs[i].mutex);
  973. }
  974. gigaset_if_initdriver(drv, procname, devname);
  975. spin_lock_irqsave(&driver_lock, flags);
  976. list_add(&drv->list, &drivers);
  977. spin_unlock_irqrestore(&driver_lock, flags);
  978. return drv;
  979. error:
  980. kfree(drv->cs);
  981. kfree(drv);
  982. return NULL;
  983. }
  984. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  985. /**
  986. * gigaset_blockdriver() - block driver
  987. * @drv: driver descriptor structure.
  988. *
  989. * Prevents the driver from attaching new devices, in preparation for
  990. * deregistration.
  991. */
  992. void gigaset_blockdriver(struct gigaset_driver *drv)
  993. {
  994. drv->blocked = 1;
  995. }
  996. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  997. static int __init gigaset_init_module(void)
  998. {
  999. /* in accordance with the principle of least astonishment,
  1000. * setting the 'debug' parameter to 1 activates a sensible
  1001. * set of default debug levels
  1002. */
  1003. if (gigaset_debuglevel == 1)
  1004. gigaset_debuglevel = DEBUG_DEFAULT;
  1005. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  1006. gigaset_isdn_regdrv();
  1007. return 0;
  1008. }
  1009. static void __exit gigaset_exit_module(void)
  1010. {
  1011. gigaset_isdn_unregdrv();
  1012. }
  1013. module_init(gigaset_init_module);
  1014. module_exit(gigaset_exit_module);
  1015. MODULE_AUTHOR(DRIVER_AUTHOR);
  1016. MODULE_DESCRIPTION(DRIVER_DESC);
  1017. MODULE_LICENSE("GPL");