usb-gigaset.c 25 KB

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