usb-gigaset.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * USB driver for Gigaset 307x directly or using M105 Data.
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers
  5. * and Hansjoerg Lipp <hjlipp@web.de>.
  6. *
  7. * This driver was derived from the USB skeleton driver by
  8. * Greg Kroah-Hartman <greg@kroah.com>
  9. *
  10. * =====================================================================
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. * =====================================================================
  16. */
  17. #include "gigaset.h"
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. /* Version Information */
  25. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  26. #define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
  27. /* Module parameters */
  28. static int startmode = SM_ISDN;
  29. static int cidmode = 1;
  30. module_param(startmode, int, S_IRUGO);
  31. module_param(cidmode, int, S_IRUGO);
  32. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  33. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  34. #define GIGASET_MINORS 1
  35. #define GIGASET_MINOR 8
  36. #define GIGASET_MODULENAME "usb_gigaset"
  37. #define GIGASET_DEVNAME "ttyGU"
  38. #define IF_WRITEBUF 2000 //FIXME // WAKEUP_CHARS: 256
  39. /* Values for the Gigaset M105 Data */
  40. #define USB_M105_VENDOR_ID 0x0681
  41. #define USB_M105_PRODUCT_ID 0x0009
  42. /* table of devices that work with this driver */
  43. static const struct usb_device_id gigaset_table [] = {
  44. { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
  45. { } /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(usb, gigaset_table);
  48. /*
  49. * Control requests (empty fields: 00)
  50. *
  51. * RT|RQ|VALUE|INDEX|LEN |DATA
  52. * In:
  53. * C1 08 01
  54. * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
  55. * C1 0F ll ll
  56. * Get device information/status (llll: 0x200 and 0x40 seen).
  57. * Real size: I only saw MIN(llll,0x64).
  58. * Contents: seems to be always the same...
  59. * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
  60. * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
  61. * rest: ?
  62. * Out:
  63. * 41 11
  64. * Initialize/reset device ?
  65. * 41 00 xx 00
  66. * ? (xx=00 or 01; 01 on start, 00 on close)
  67. * 41 07 vv mm
  68. * Set/clear flags vv=value, mm=mask (see RQ 08)
  69. * 41 12 xx
  70. * Used before the following configuration requests are issued
  71. * (with xx=0x0f). I've seen other values<0xf, though.
  72. * 41 01 xx xx
  73. * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
  74. * 41 03 ps bb
  75. * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
  76. * [ 0x30: m, 0x40: s ]
  77. * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
  78. * bb: bits/byte (seen 7 and 8)
  79. * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
  80. * ??
  81. * Initialization: 01, 40, 00, 00
  82. * Open device: 00 40, 00, 00
  83. * yy and zz seem to be equal, either 0x00 or 0x0a
  84. * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
  85. * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
  86. * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
  87. * xx is usually 0x00 but was 0x7e before starting data transfer
  88. * in unimodem mode. So, this might be an array of characters that need
  89. * special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
  90. *
  91. * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
  92. * flags per packet.
  93. */
  94. static int gigaset_probe(struct usb_interface *interface,
  95. const struct usb_device_id *id);
  96. static void gigaset_disconnect(struct usb_interface *interface);
  97. static struct gigaset_driver *driver = NULL;
  98. static struct cardstate *cardstate = NULL;
  99. /* usb specific object needed to register this driver with the usb subsystem */
  100. static struct usb_driver gigaset_usb_driver = {
  101. .name = GIGASET_MODULENAME,
  102. .probe = gigaset_probe,
  103. .disconnect = gigaset_disconnect,
  104. .id_table = gigaset_table,
  105. };
  106. struct usb_cardstate {
  107. struct usb_device *udev; /* usb device pointer */
  108. struct usb_interface *interface; /* interface for this device */
  109. atomic_t busy; /* bulk output in progress */
  110. /* Output buffer */
  111. unsigned char *bulk_out_buffer;
  112. int bulk_out_size;
  113. __u8 bulk_out_endpointAddr;
  114. struct urb *bulk_out_urb;
  115. /* Input buffer */
  116. int rcvbuf_size;
  117. struct urb *read_urb;
  118. __u8 int_in_endpointAddr;
  119. char bchars[6]; /* for request 0x19 */
  120. };
  121. static inline unsigned tiocm_to_gigaset(unsigned state)
  122. {
  123. return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
  124. }
  125. #ifdef CONFIG_GIGASET_UNDOCREQ
  126. /* WARNING: EXPERIMENTAL! */
  127. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  128. unsigned new_state)
  129. {
  130. struct usb_device *udev = cs->hw.usb->udev;
  131. unsigned mask, val;
  132. int r;
  133. mask = tiocm_to_gigaset(old_state ^ new_state);
  134. val = tiocm_to_gigaset(new_state);
  135. gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
  136. // don't use this in an interrupt/BH
  137. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
  138. (val & 0xff) | ((mask & 0xff) << 8), 0,
  139. NULL, 0, 2000 /* timeout? */);
  140. if (r < 0)
  141. return r;
  142. //..
  143. return 0;
  144. }
  145. static int set_value(struct cardstate *cs, u8 req, u16 val)
  146. {
  147. struct usb_device *udev = cs->hw.usb->udev;
  148. int r, r2;
  149. gig_dbg(DEBUG_USBREQ, "request %02x (%04x)",
  150. (unsigned)req, (unsigned)val);
  151. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41,
  152. 0xf /*?*/, 0, NULL, 0, 2000 /*?*/);
  153. /* no idea what this does */
  154. if (r < 0) {
  155. dev_err(&udev->dev, "error %d on request 0x12\n", -r);
  156. return r;
  157. }
  158. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
  159. val, 0, NULL, 0, 2000 /*?*/);
  160. if (r < 0)
  161. dev_err(&udev->dev, "error %d on request 0x%02x\n",
  162. -r, (unsigned)req);
  163. r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  164. 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
  165. if (r2 < 0)
  166. dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
  167. return r < 0 ? r : (r2 < 0 ? r2 : 0);
  168. }
  169. /* WARNING: HIGHLY EXPERIMENTAL! */
  170. // don't use this in an interrupt/BH
  171. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  172. {
  173. u16 val;
  174. u32 rate;
  175. cflag &= CBAUD;
  176. switch (cflag) {
  177. //FIXME more values?
  178. case B300: rate = 300; break;
  179. case B600: rate = 600; break;
  180. case B1200: rate = 1200; break;
  181. case B2400: rate = 2400; break;
  182. case B4800: rate = 4800; break;
  183. case B9600: rate = 9600; break;
  184. case B19200: rate = 19200; break;
  185. case B38400: rate = 38400; break;
  186. case B57600: rate = 57600; break;
  187. case B115200: rate = 115200; break;
  188. default:
  189. rate = 9600;
  190. dev_err(cs->dev, "unsupported baudrate request 0x%x,"
  191. " using default of B9600\n", cflag);
  192. }
  193. val = 0x383fff / rate + 1;
  194. return set_value(cs, 1, val);
  195. }
  196. /* WARNING: HIGHLY EXPERIMENTAL! */
  197. // don't use this in an interrupt/BH
  198. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  199. {
  200. u16 val = 0;
  201. /* set the parity */
  202. if (cflag & PARENB)
  203. val |= (cflag & PARODD) ? 0x10 : 0x20;
  204. /* set the number of data bits */
  205. switch (cflag & CSIZE) {
  206. case CS5:
  207. val |= 5 << 8; break;
  208. case CS6:
  209. val |= 6 << 8; break;
  210. case CS7:
  211. val |= 7 << 8; break;
  212. case CS8:
  213. val |= 8 << 8; break;
  214. default:
  215. dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
  216. val |= 8 << 8;
  217. break;
  218. }
  219. /* set the number of stop bits */
  220. if (cflag & CSTOPB) {
  221. if ((cflag & CSIZE) == CS5)
  222. val |= 1; /* 1.5 stop bits */ //FIXME is this okay?
  223. else
  224. val |= 2; /* 2 stop bits */
  225. }
  226. return set_value(cs, 3, val);
  227. }
  228. #else
  229. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  230. unsigned new_state)
  231. {
  232. return -EINVAL;
  233. }
  234. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  235. {
  236. return -EINVAL;
  237. }
  238. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  239. {
  240. return -EINVAL;
  241. }
  242. #endif
  243. /*================================================================================================================*/
  244. static int gigaset_init_bchannel(struct bc_state *bcs)
  245. {
  246. /* nothing to do for M10x */
  247. gigaset_bchannel_up(bcs);
  248. return 0;
  249. }
  250. static int gigaset_close_bchannel(struct bc_state *bcs)
  251. {
  252. /* nothing to do for M10x */
  253. gigaset_bchannel_down(bcs);
  254. return 0;
  255. }
  256. static int write_modem(struct cardstate *cs);
  257. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
  258. /* Write tasklet handler: Continue sending current skb, or send command, or
  259. * start sending an skb from the send queue.
  260. */
  261. static void gigaset_modem_fill(unsigned long data)
  262. {
  263. struct cardstate *cs = (struct cardstate *) data;
  264. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  265. struct cmdbuf_t *cb;
  266. unsigned long flags;
  267. int again;
  268. gig_dbg(DEBUG_OUTPUT, "modem_fill");
  269. if (atomic_read(&cs->hw.usb->busy)) {
  270. gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
  271. return;
  272. }
  273. do {
  274. again = 0;
  275. if (!bcs->tx_skb) { /* no skb is being sent */
  276. spin_lock_irqsave(&cs->cmdlock, flags);
  277. cb = cs->cmdbuf;
  278. spin_unlock_irqrestore(&cs->cmdlock, flags);
  279. if (cb) { /* commands to send? */
  280. gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
  281. if (send_cb(cs, cb) < 0) {
  282. gig_dbg(DEBUG_OUTPUT,
  283. "modem_fill: send_cb failed");
  284. again = 1; /* no callback will be
  285. called! */
  286. }
  287. } else { /* skbs to send? */
  288. bcs->tx_skb = skb_dequeue(&bcs->squeue);
  289. if (bcs->tx_skb)
  290. gig_dbg(DEBUG_INTR,
  291. "Dequeued skb (Adr: %lx)!",
  292. (unsigned long) bcs->tx_skb);
  293. }
  294. }
  295. if (bcs->tx_skb) {
  296. gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
  297. if (write_modem(cs) < 0) {
  298. gig_dbg(DEBUG_OUTPUT,
  299. "modem_fill: write_modem failed");
  300. // FIXME should we tell the LL?
  301. again = 1; /* no callback will be called! */
  302. }
  303. }
  304. } while (again);
  305. }
  306. /**
  307. * gigaset_read_int_callback
  308. *
  309. * It is called if the data was received from the device.
  310. */
  311. static void gigaset_read_int_callback(struct urb *urb)
  312. {
  313. struct inbuf_t *inbuf = urb->context;
  314. struct cardstate *cs = inbuf->cs;
  315. int resubmit = 0;
  316. int r;
  317. unsigned numbytes;
  318. unsigned char *src;
  319. unsigned long flags;
  320. if (!urb->status) {
  321. if (!cs->connected) {
  322. err("%s: disconnected", __func__); /* should never happen */
  323. return;
  324. }
  325. numbytes = urb->actual_length;
  326. if (numbytes) {
  327. src = inbuf->rcvbuf;
  328. if (unlikely(*src))
  329. dev_warn(cs->dev,
  330. "%s: There was no leading 0, but 0x%02x!\n",
  331. __func__, (unsigned) *src);
  332. ++src; /* skip leading 0x00 */
  333. --numbytes;
  334. if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
  335. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  336. gigaset_schedule_event(inbuf->cs);
  337. }
  338. } else
  339. gig_dbg(DEBUG_INTR, "Received zero block length");
  340. resubmit = 1;
  341. } else {
  342. /* The urb might have been killed. */
  343. gig_dbg(DEBUG_ANY, "%s - nonzero read bulk status received: %d",
  344. __func__, urb->status);
  345. if (urb->status != -ENOENT) { /* not killed */
  346. if (!cs->connected) {
  347. err("%s: disconnected", __func__); /* should never happen */
  348. return;
  349. }
  350. resubmit = 1;
  351. }
  352. }
  353. if (resubmit) {
  354. spin_lock_irqsave(&cs->lock, flags);
  355. r = cs->connected ? usb_submit_urb(urb, GFP_ATOMIC) : -ENODEV;
  356. spin_unlock_irqrestore(&cs->lock, flags);
  357. if (r)
  358. dev_err(cs->dev, "error %d when resubmitting urb.\n",
  359. -r);
  360. }
  361. }
  362. /* This callback routine is called when data was transmitted to the device. */
  363. static void gigaset_write_bulk_callback(struct urb *urb)
  364. {
  365. struct cardstate *cs = urb->context;
  366. unsigned long flags;
  367. if (urb->status)
  368. dev_err(cs->dev, "bulk transfer failed (status %d)\n",
  369. -urb->status);
  370. /* That's all we can do. Communication problems
  371. are handled by timeouts or network protocols. */
  372. spin_lock_irqsave(&cs->lock, flags);
  373. if (!cs->connected) {
  374. err("%s: not connected", __func__);
  375. } else {
  376. atomic_set(&cs->hw.usb->busy, 0);
  377. tasklet_schedule(&cs->write_tasklet);
  378. }
  379. spin_unlock_irqrestore(&cs->lock, flags);
  380. }
  381. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
  382. {
  383. struct cmdbuf_t *tcb;
  384. unsigned long flags;
  385. int count;
  386. int status = -ENOENT; // FIXME
  387. struct usb_cardstate *ucs = cs->hw.usb;
  388. do {
  389. if (!cb->len) {
  390. tcb = cb;
  391. spin_lock_irqsave(&cs->cmdlock, flags);
  392. cs->cmdbytes -= cs->curlen;
  393. gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
  394. cs->curlen, cs->cmdbytes);
  395. cs->cmdbuf = cb = cb->next;
  396. if (cb) {
  397. cb->prev = NULL;
  398. cs->curlen = cb->len;
  399. } else {
  400. cs->lastcmdbuf = NULL;
  401. cs->curlen = 0;
  402. }
  403. spin_unlock_irqrestore(&cs->cmdlock, flags);
  404. if (tcb->wake_tasklet)
  405. tasklet_schedule(tcb->wake_tasklet);
  406. kfree(tcb);
  407. }
  408. if (cb) {
  409. count = min(cb->len, ucs->bulk_out_size);
  410. gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
  411. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  412. usb_sndbulkpipe(ucs->udev,
  413. ucs->bulk_out_endpointAddr & 0x0f),
  414. cb->buf + cb->offset, count,
  415. gigaset_write_bulk_callback, cs);
  416. cb->offset += count;
  417. cb->len -= count;
  418. atomic_set(&ucs->busy, 1);
  419. spin_lock_irqsave(&cs->lock, flags);
  420. status = cs->connected ? usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC) : -ENODEV;
  421. spin_unlock_irqrestore(&cs->lock, flags);
  422. if (status) {
  423. atomic_set(&ucs->busy, 0);
  424. err("could not submit urb (error %d)\n",
  425. -status);
  426. cb->len = 0; /* skip urb => remove cb+wakeup
  427. in next loop cycle */
  428. }
  429. }
  430. } while (cb && status); /* next command on error */
  431. return status;
  432. }
  433. /* Send command to device. */
  434. static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
  435. int len, struct tasklet_struct *wake_tasklet)
  436. {
  437. struct cmdbuf_t *cb;
  438. unsigned long flags;
  439. gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
  440. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  441. "CMD Transmit", len, buf);
  442. if (len <= 0)
  443. return 0;
  444. if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
  445. dev_err(cs->dev, "%s: out of memory\n", __func__);
  446. return -ENOMEM;
  447. }
  448. memcpy(cb->buf, buf, len);
  449. cb->len = len;
  450. cb->offset = 0;
  451. cb->next = NULL;
  452. cb->wake_tasklet = wake_tasklet;
  453. spin_lock_irqsave(&cs->cmdlock, flags);
  454. cb->prev = cs->lastcmdbuf;
  455. if (cs->lastcmdbuf)
  456. cs->lastcmdbuf->next = cb;
  457. else {
  458. cs->cmdbuf = cb;
  459. cs->curlen = len;
  460. }
  461. cs->cmdbytes += len;
  462. cs->lastcmdbuf = cb;
  463. spin_unlock_irqrestore(&cs->cmdlock, flags);
  464. spin_lock_irqsave(&cs->lock, flags);
  465. if (cs->connected)
  466. tasklet_schedule(&cs->write_tasklet);
  467. spin_unlock_irqrestore(&cs->lock, flags);
  468. return len;
  469. }
  470. static int gigaset_write_room(struct cardstate *cs)
  471. {
  472. unsigned long flags;
  473. unsigned bytes;
  474. spin_lock_irqsave(&cs->cmdlock, flags);
  475. bytes = cs->cmdbytes;
  476. spin_unlock_irqrestore(&cs->cmdlock, flags);
  477. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  478. }
  479. static int gigaset_chars_in_buffer(struct cardstate *cs)
  480. {
  481. return cs->cmdbytes;
  482. }
  483. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  484. {
  485. #ifdef CONFIG_GIGASET_UNDOCREQ
  486. struct usb_device *udev = cs->hw.usb->udev;
  487. gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
  488. memcpy(cs->hw.usb->bchars, buf, 6);
  489. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  490. 0, 0, &buf, 6, 2000);
  491. #else
  492. return -EINVAL;
  493. #endif
  494. }
  495. static int gigaset_freebcshw(struct bc_state *bcs)
  496. {
  497. /* unused */
  498. return 1;
  499. }
  500. /* Initialize the b-channel structure */
  501. static int gigaset_initbcshw(struct bc_state *bcs)
  502. {
  503. /* unused */
  504. bcs->hw.usb = NULL;
  505. return 1;
  506. }
  507. static void gigaset_reinitbcshw(struct bc_state *bcs)
  508. {
  509. /* nothing to do for M10x */
  510. }
  511. static void gigaset_freecshw(struct cardstate *cs)
  512. {
  513. tasklet_kill(&cs->write_tasklet);
  514. kfree(cs->hw.usb);
  515. }
  516. static int gigaset_initcshw(struct cardstate *cs)
  517. {
  518. struct usb_cardstate *ucs;
  519. cs->hw.usb = ucs =
  520. kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
  521. if (!ucs)
  522. return 0;
  523. ucs->bchars[0] = 0;
  524. ucs->bchars[1] = 0;
  525. ucs->bchars[2] = 0;
  526. ucs->bchars[3] = 0;
  527. ucs->bchars[4] = 0x11;
  528. ucs->bchars[5] = 0x13;
  529. ucs->bulk_out_buffer = NULL;
  530. ucs->bulk_out_urb = NULL;
  531. //ucs->urb_cmd_out = NULL;
  532. ucs->read_urb = NULL;
  533. tasklet_init(&cs->write_tasklet,
  534. &gigaset_modem_fill, (unsigned long) cs);
  535. return 1;
  536. }
  537. /* Send data from current skb to the device. */
  538. static int write_modem(struct cardstate *cs)
  539. {
  540. int ret = 0;
  541. int count;
  542. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  543. struct usb_cardstate *ucs = cs->hw.usb;
  544. unsigned long flags;
  545. gig_dbg(DEBUG_WRITE, "len: %d...", bcs->tx_skb->len);
  546. if (!bcs->tx_skb->len) {
  547. dev_kfree_skb_any(bcs->tx_skb);
  548. bcs->tx_skb = NULL;
  549. return -EINVAL;
  550. }
  551. /* Copy data to bulk out buffer and // FIXME copying not necessary
  552. * transmit data
  553. */
  554. count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
  555. skb_copy_from_linear_data(bcs->tx_skb, ucs->bulk_out_buffer, count);
  556. skb_pull(bcs->tx_skb, count);
  557. atomic_set(&ucs->busy, 1);
  558. gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
  559. spin_lock_irqsave(&cs->lock, flags);
  560. if (cs->connected) {
  561. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  562. usb_sndbulkpipe(ucs->udev,
  563. ucs->bulk_out_endpointAddr & 0x0f),
  564. ucs->bulk_out_buffer, count,
  565. gigaset_write_bulk_callback, cs);
  566. ret = usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC);
  567. } else {
  568. ret = -ENODEV;
  569. }
  570. spin_unlock_irqrestore(&cs->lock, flags);
  571. if (ret) {
  572. err("could not submit urb (error %d)\n", -ret);
  573. atomic_set(&ucs->busy, 0);
  574. }
  575. if (!bcs->tx_skb->len) {
  576. /* skb sent completely */
  577. gigaset_skb_sent(bcs, bcs->tx_skb); //FIXME also, when ret<0?
  578. gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
  579. (unsigned long) bcs->tx_skb);
  580. dev_kfree_skb_any(bcs->tx_skb);
  581. bcs->tx_skb = NULL;
  582. }
  583. return ret;
  584. }
  585. static int gigaset_probe(struct usb_interface *interface,
  586. const struct usb_device_id *id)
  587. {
  588. int retval;
  589. struct usb_device *udev = interface_to_usbdev(interface);
  590. unsigned int ifnum;
  591. struct usb_host_interface *hostif;
  592. struct cardstate *cs = NULL;
  593. struct usb_cardstate *ucs = NULL;
  594. struct usb_endpoint_descriptor *endpoint;
  595. int buffer_size;
  596. int alt;
  597. gig_dbg(DEBUG_ANY,
  598. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  599. __func__, le16_to_cpu(udev->descriptor.idVendor),
  600. le16_to_cpu(udev->descriptor.idProduct));
  601. retval = -ENODEV; //FIXME
  602. /* See if the device offered us matches what we can accept */
  603. if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) ||
  604. (le16_to_cpu(udev->descriptor.idProduct) != USB_M105_PRODUCT_ID))
  605. return -ENODEV;
  606. /* this starts to become ascii art... */
  607. hostif = interface->cur_altsetting;
  608. alt = hostif->desc.bAlternateSetting;
  609. ifnum = hostif->desc.bInterfaceNumber; // FIXME ?
  610. if (alt != 0 || ifnum != 0) {
  611. dev_warn(&udev->dev, "ifnum %d, alt %d\n", ifnum, alt);
  612. return -ENODEV;
  613. }
  614. /* Reject application specific intefaces
  615. *
  616. */
  617. if (hostif->desc.bInterfaceClass != 255) {
  618. dev_info(&udev->dev,
  619. "%s: Device matched but iface_desc[%d]->bInterfaceClass==%d!\n",
  620. __func__, ifnum, hostif->desc.bInterfaceClass);
  621. return -ENODEV;
  622. }
  623. dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
  624. cs = gigaset_getunassignedcs(driver);
  625. if (!cs) {
  626. dev_warn(&udev->dev, "no free cardstate\n");
  627. return -ENODEV;
  628. }
  629. ucs = cs->hw.usb;
  630. /* save off device structure ptrs for later use */
  631. usb_get_dev(udev);
  632. ucs->udev = udev;
  633. ucs->interface = interface;
  634. cs->dev = &interface->dev;
  635. /* save address of controller structure */
  636. usb_set_intfdata(interface, cs); // dev_set_drvdata(&interface->dev, cs);
  637. endpoint = &hostif->endpoint[0].desc;
  638. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  639. ucs->bulk_out_size = buffer_size;
  640. ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  641. ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
  642. if (!ucs->bulk_out_buffer) {
  643. dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
  644. retval = -ENOMEM;
  645. goto error;
  646. }
  647. ucs->bulk_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  648. if (!ucs->bulk_out_urb) {
  649. dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
  650. retval = -ENOMEM;
  651. goto error;
  652. }
  653. endpoint = &hostif->endpoint[1].desc;
  654. atomic_set(&ucs->busy, 0);
  655. ucs->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  656. if (!ucs->read_urb) {
  657. dev_err(cs->dev, "No free urbs available\n");
  658. retval = -ENOMEM;
  659. goto error;
  660. }
  661. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  662. ucs->rcvbuf_size = buffer_size;
  663. ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
  664. cs->inbuf[0].rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
  665. if (!cs->inbuf[0].rcvbuf) {
  666. dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
  667. retval = -ENOMEM;
  668. goto error;
  669. }
  670. /* Fill the interrupt urb and send it to the core */
  671. usb_fill_int_urb(ucs->read_urb, udev,
  672. usb_rcvintpipe(udev,
  673. endpoint->bEndpointAddress & 0x0f),
  674. cs->inbuf[0].rcvbuf, buffer_size,
  675. gigaset_read_int_callback,
  676. cs->inbuf + 0, endpoint->bInterval);
  677. retval = usb_submit_urb(ucs->read_urb, GFP_KERNEL);
  678. if (retval) {
  679. dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
  680. goto error;
  681. }
  682. /* tell common part that the device is ready */
  683. if (startmode == SM_LOCKED)
  684. atomic_set(&cs->mstate, MS_LOCKED);
  685. if (!gigaset_start(cs)) {
  686. tasklet_kill(&cs->write_tasklet);
  687. retval = -ENODEV; //FIXME
  688. goto error;
  689. }
  690. return 0;
  691. error:
  692. usb_kill_urb(ucs->read_urb);
  693. kfree(ucs->bulk_out_buffer);
  694. usb_free_urb(ucs->bulk_out_urb);
  695. kfree(cs->inbuf[0].rcvbuf);
  696. usb_free_urb(ucs->read_urb);
  697. usb_set_intfdata(interface, NULL);
  698. ucs->read_urb = ucs->bulk_out_urb = NULL;
  699. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  700. usb_put_dev(ucs->udev);
  701. ucs->udev = NULL;
  702. ucs->interface = NULL;
  703. gigaset_unassign(cs);
  704. return retval;
  705. }
  706. static void gigaset_disconnect(struct usb_interface *interface)
  707. {
  708. struct cardstate *cs;
  709. struct usb_cardstate *ucs;
  710. cs = usb_get_intfdata(interface);
  711. ucs = cs->hw.usb;
  712. usb_kill_urb(ucs->read_urb);
  713. gigaset_stop(cs);
  714. usb_set_intfdata(interface, NULL);
  715. tasklet_kill(&cs->write_tasklet);
  716. usb_kill_urb(ucs->bulk_out_urb); /* FIXME: only if active? */
  717. kfree(ucs->bulk_out_buffer);
  718. usb_free_urb(ucs->bulk_out_urb);
  719. kfree(cs->inbuf[0].rcvbuf);
  720. usb_free_urb(ucs->read_urb);
  721. ucs->read_urb = ucs->bulk_out_urb = NULL;
  722. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  723. usb_put_dev(ucs->udev);
  724. ucs->interface = NULL;
  725. ucs->udev = NULL;
  726. cs->dev = NULL;
  727. gigaset_unassign(cs);
  728. }
  729. static const struct gigaset_ops ops = {
  730. gigaset_write_cmd,
  731. gigaset_write_room,
  732. gigaset_chars_in_buffer,
  733. gigaset_brkchars,
  734. gigaset_init_bchannel,
  735. gigaset_close_bchannel,
  736. gigaset_initbcshw,
  737. gigaset_freebcshw,
  738. gigaset_reinitbcshw,
  739. gigaset_initcshw,
  740. gigaset_freecshw,
  741. gigaset_set_modem_ctrl,
  742. gigaset_baud_rate,
  743. gigaset_set_line_ctrl,
  744. gigaset_m10x_send_skb,
  745. gigaset_m10x_input,
  746. };
  747. /**
  748. * usb_gigaset_init
  749. * This function is called while kernel-module is loaded
  750. */
  751. static int __init usb_gigaset_init(void)
  752. {
  753. int result;
  754. /* allocate memory for our driver state and intialize it */
  755. if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  756. GIGASET_MODULENAME, GIGASET_DEVNAME,
  757. &ops, THIS_MODULE)) == NULL)
  758. goto error;
  759. /* allocate memory for our device state and intialize it */
  760. cardstate = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  761. if (!cardstate)
  762. goto error;
  763. /* register this driver with the USB subsystem */
  764. result = usb_register(&gigaset_usb_driver);
  765. if (result < 0) {
  766. err("usb_gigaset: usb_register failed (error %d)",
  767. -result);
  768. goto error;
  769. }
  770. info(DRIVER_AUTHOR);
  771. info(DRIVER_DESC);
  772. return 0;
  773. error: if (cardstate)
  774. gigaset_freecs(cardstate);
  775. cardstate = NULL;
  776. if (driver)
  777. gigaset_freedriver(driver);
  778. driver = NULL;
  779. return -1;
  780. }
  781. /**
  782. * usb_gigaset_exit
  783. * This function is called while unloading the kernel-module
  784. */
  785. static void __exit usb_gigaset_exit(void)
  786. {
  787. gigaset_blockdriver(driver); /* => probe will fail
  788. * => no gigaset_start any more
  789. */
  790. gigaset_shutdown(cardstate);
  791. /* from now on, no isdn callback should be possible */
  792. /* deregister this driver with the USB subsystem */
  793. usb_deregister(&gigaset_usb_driver);
  794. /* this will call the disconnect-callback */
  795. /* from now on, no disconnect/probe callback should be running */
  796. gigaset_freecs(cardstate);
  797. cardstate = NULL;
  798. gigaset_freedriver(driver);
  799. driver = NULL;
  800. }
  801. module_init(usb_gigaset_init);
  802. module_exit(usb_gigaset_exit);
  803. MODULE_AUTHOR(DRIVER_AUTHOR);
  804. MODULE_DESCRIPTION(DRIVER_DESC);
  805. MODULE_LICENSE("GPL");