bas-gigaset.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  1. /*
  2. * USB driver for Gigaset 307x base via direct USB connection.
  3. *
  4. * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
  5. * Tilman Schmidt <tilman@imap.cc>,
  6. * Stefan Eilers.
  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/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/timer.h>
  20. #include <linux/usb.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. /* Version Information */
  24. #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  25. #define DRIVER_DESC "USB Driver for Gigaset 307x"
  26. /* Module parameters */
  27. static int startmode = SM_ISDN;
  28. static int cidmode = 1;
  29. module_param(startmode, int, S_IRUGO);
  30. module_param(cidmode, int, S_IRUGO);
  31. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  32. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  33. #define GIGASET_MINORS 1
  34. #define GIGASET_MINOR 16
  35. #define GIGASET_MODULENAME "bas_gigaset"
  36. #define GIGASET_DEVNAME "ttyGB"
  37. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  38. #define IF_WRITEBUF 264
  39. /* interrupt pipe message size according to ibid. ch. 2.2 */
  40. #define IP_MSGSIZE 3
  41. /* Values for the Gigaset 307x */
  42. #define USB_GIGA_VENDOR_ID 0x0681
  43. #define USB_3070_PRODUCT_ID 0x0001
  44. #define USB_3075_PRODUCT_ID 0x0002
  45. #define USB_SX303_PRODUCT_ID 0x0021
  46. #define USB_SX353_PRODUCT_ID 0x0022
  47. /* table of devices that work with this driver */
  48. static const struct usb_device_id gigaset_table [] = {
  49. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
  50. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
  51. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
  52. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
  53. { } /* Terminating entry */
  54. };
  55. MODULE_DEVICE_TABLE(usb, gigaset_table);
  56. /*======================= local function prototypes ==========================*/
  57. /* function called if a new device belonging to this driver is connected */
  58. static int gigaset_probe(struct usb_interface *interface,
  59. const struct usb_device_id *id);
  60. /* Function will be called if the device is unplugged */
  61. static void gigaset_disconnect(struct usb_interface *interface);
  62. /* functions called before/after suspend */
  63. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
  64. static int gigaset_resume(struct usb_interface *intf);
  65. /* functions called before/after device reset */
  66. static int gigaset_pre_reset(struct usb_interface *intf);
  67. static int gigaset_post_reset(struct usb_interface *intf);
  68. static int atread_submit(struct cardstate *, int);
  69. static void stopurbs(struct bas_bc_state *);
  70. static int req_submit(struct bc_state *, int, int, int);
  71. static int atwrite_submit(struct cardstate *, unsigned char *, int);
  72. static int start_cbsend(struct cardstate *);
  73. /*============================================================================*/
  74. struct bas_cardstate {
  75. struct usb_device *udev; /* USB device pointer */
  76. struct usb_interface *interface; /* interface for this device */
  77. unsigned char minor; /* starting minor number */
  78. struct urb *urb_ctrl; /* control pipe default URB */
  79. struct usb_ctrlrequest dr_ctrl;
  80. struct timer_list timer_ctrl; /* control request timeout */
  81. int retry_ctrl;
  82. struct timer_list timer_atrdy; /* AT command ready timeout */
  83. struct urb *urb_cmd_out; /* for sending AT commands */
  84. struct usb_ctrlrequest dr_cmd_out;
  85. int retry_cmd_out;
  86. struct urb *urb_cmd_in; /* for receiving AT replies */
  87. struct usb_ctrlrequest dr_cmd_in;
  88. struct timer_list timer_cmd_in; /* receive request timeout */
  89. unsigned char *rcvbuf; /* AT reply receive buffer */
  90. struct urb *urb_int_in; /* URB for interrupt pipe */
  91. unsigned char *int_in_buf;
  92. spinlock_t lock; /* locks all following */
  93. int basstate; /* bitmap (BS_*) */
  94. int pending; /* uncompleted base request */
  95. wait_queue_head_t waitqueue;
  96. int rcvbuf_size; /* size of AT receive buffer */
  97. /* 0: no receive in progress */
  98. int retry_cmd_in; /* receive req retry count */
  99. };
  100. /* status of direct USB connection to 307x base (bits in basstate) */
  101. #define BS_ATOPEN 0x001 /* AT channel open */
  102. #define BS_B1OPEN 0x002 /* B channel 1 open */
  103. #define BS_B2OPEN 0x004 /* B channel 2 open */
  104. #define BS_ATREADY 0x008 /* base ready for AT command */
  105. #define BS_INIT 0x010 /* base has signalled INIT_OK */
  106. #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
  107. #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
  108. #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
  109. #define BS_SUSPEND 0x100 /* USB port suspended */
  110. #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
  111. static struct gigaset_driver *driver = NULL;
  112. /* usb specific object needed to register this driver with the usb subsystem */
  113. static struct usb_driver gigaset_usb_driver = {
  114. .name = GIGASET_MODULENAME,
  115. .probe = gigaset_probe,
  116. .disconnect = gigaset_disconnect,
  117. .id_table = gigaset_table,
  118. .suspend = gigaset_suspend,
  119. .resume = gigaset_resume,
  120. .reset_resume = gigaset_post_reset,
  121. .pre_reset = gigaset_pre_reset,
  122. .post_reset = gigaset_post_reset,
  123. };
  124. /* get message text for usb_submit_urb return code
  125. */
  126. static char *get_usb_rcmsg(int rc)
  127. {
  128. static char unkmsg[28];
  129. switch (rc) {
  130. case 0:
  131. return "success";
  132. case -ENOMEM:
  133. return "out of memory";
  134. case -ENODEV:
  135. return "device not present";
  136. case -ENOENT:
  137. return "endpoint not present";
  138. case -ENXIO:
  139. return "URB type not supported";
  140. case -EINVAL:
  141. return "invalid argument";
  142. case -EAGAIN:
  143. return "start frame too early or too much scheduled";
  144. case -EFBIG:
  145. return "too many isochronous frames requested";
  146. case -EPIPE:
  147. return "endpoint stalled";
  148. case -EMSGSIZE:
  149. return "invalid packet size";
  150. case -ENOSPC:
  151. return "would overcommit USB bandwidth";
  152. case -ESHUTDOWN:
  153. return "device shut down";
  154. case -EPERM:
  155. return "reject flag set";
  156. case -EHOSTUNREACH:
  157. return "device suspended";
  158. default:
  159. snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
  160. return unkmsg;
  161. }
  162. }
  163. /* get message text for USB status code
  164. */
  165. static char *get_usb_statmsg(int status)
  166. {
  167. static char unkmsg[28];
  168. switch (status) {
  169. case 0:
  170. return "success";
  171. case -ENOENT:
  172. return "unlinked (sync)";
  173. case -EINPROGRESS:
  174. return "pending";
  175. case -EPROTO:
  176. return "bit stuffing error, timeout, or unknown USB error";
  177. case -EILSEQ:
  178. return "CRC mismatch, timeout, or unknown USB error";
  179. case -ETIME:
  180. return "timed out";
  181. case -EPIPE:
  182. return "endpoint stalled";
  183. case -ECOMM:
  184. return "IN buffer overrun";
  185. case -ENOSR:
  186. return "OUT buffer underrun";
  187. case -EOVERFLOW:
  188. return "too much data";
  189. case -EREMOTEIO:
  190. return "short packet detected";
  191. case -ENODEV:
  192. return "device removed";
  193. case -EXDEV:
  194. return "partial isochronous transfer";
  195. case -EINVAL:
  196. return "invalid argument";
  197. case -ECONNRESET:
  198. return "unlinked (async)";
  199. case -ESHUTDOWN:
  200. return "device shut down";
  201. default:
  202. snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
  203. return unkmsg;
  204. }
  205. }
  206. /* usb_pipetype_str
  207. * retrieve string representation of USB pipe type
  208. */
  209. static inline char *usb_pipetype_str(int pipe)
  210. {
  211. if (usb_pipeisoc(pipe))
  212. return "Isoc";
  213. if (usb_pipeint(pipe))
  214. return "Int";
  215. if (usb_pipecontrol(pipe))
  216. return "Ctrl";
  217. if (usb_pipebulk(pipe))
  218. return "Bulk";
  219. return "?";
  220. }
  221. /* dump_urb
  222. * write content of URB to syslog for debugging
  223. */
  224. static inline void dump_urb(enum debuglevel level, const char *tag,
  225. struct urb *urb)
  226. {
  227. #ifdef CONFIG_GIGASET_DEBUG
  228. int i;
  229. gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
  230. if (urb) {
  231. gig_dbg(level,
  232. " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
  233. "hcpriv=0x%08lx, transfer_flags=0x%x,",
  234. (unsigned long) urb->dev,
  235. usb_pipetype_str(urb->pipe),
  236. usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
  237. usb_pipein(urb->pipe) ? "in" : "out",
  238. (unsigned long) urb->hcpriv,
  239. urb->transfer_flags);
  240. gig_dbg(level,
  241. " transfer_buffer=0x%08lx[%d], actual_length=%d, "
  242. "setup_packet=0x%08lx,",
  243. (unsigned long) urb->transfer_buffer,
  244. urb->transfer_buffer_length, urb->actual_length,
  245. (unsigned long) urb->setup_packet);
  246. gig_dbg(level,
  247. " start_frame=%d, number_of_packets=%d, interval=%d, "
  248. "error_count=%d,",
  249. urb->start_frame, urb->number_of_packets, urb->interval,
  250. urb->error_count);
  251. gig_dbg(level,
  252. " context=0x%08lx, complete=0x%08lx, "
  253. "iso_frame_desc[]={",
  254. (unsigned long) urb->context,
  255. (unsigned long) urb->complete);
  256. for (i = 0; i < urb->number_of_packets; i++) {
  257. struct usb_iso_packet_descriptor *pifd
  258. = &urb->iso_frame_desc[i];
  259. gig_dbg(level,
  260. " {offset=%u, length=%u, actual_length=%u, "
  261. "status=%u}",
  262. pifd->offset, pifd->length, pifd->actual_length,
  263. pifd->status);
  264. }
  265. }
  266. gig_dbg(level, "}}");
  267. #endif
  268. }
  269. /* read/set modem control bits etc. (m10x only) */
  270. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  271. unsigned new_state)
  272. {
  273. return -EINVAL;
  274. }
  275. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  276. {
  277. return -EINVAL;
  278. }
  279. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  280. {
  281. return -EINVAL;
  282. }
  283. /* set/clear bits in base connection state, return previous state
  284. */
  285. static inline int update_basstate(struct bas_cardstate *ucs,
  286. int set, int clear)
  287. {
  288. unsigned long flags;
  289. int state;
  290. spin_lock_irqsave(&ucs->lock, flags);
  291. state = ucs->basstate;
  292. ucs->basstate = (state & ~clear) | set;
  293. spin_unlock_irqrestore(&ucs->lock, flags);
  294. return state;
  295. }
  296. /* error_hangup
  297. * hang up any existing connection because of an unrecoverable error
  298. * This function may be called from any context and takes care of scheduling
  299. * the necessary actions for execution outside of interrupt context.
  300. * cs->lock must not be held.
  301. * argument:
  302. * B channel control structure
  303. */
  304. static inline void error_hangup(struct bc_state *bcs)
  305. {
  306. struct cardstate *cs = bcs->cs;
  307. gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
  308. __func__, bcs->channel);
  309. if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
  310. dev_err(cs->dev, "event queue full\n");
  311. gigaset_schedule_event(cs);
  312. }
  313. /* error_reset
  314. * reset Gigaset device because of an unrecoverable error
  315. * This function may be called from any context, and takes care of
  316. * scheduling the necessary actions for execution outside of interrupt context.
  317. * cs->lock must not be held.
  318. * argument:
  319. * controller state structure
  320. */
  321. static inline void error_reset(struct cardstate *cs)
  322. {
  323. /* reset interrupt pipe to recover (ignore errors) */
  324. update_basstate(cs->hw.bas, BS_RESETTING, 0);
  325. req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
  326. }
  327. /* check_pending
  328. * check for completion of pending control request
  329. * parameter:
  330. * ucs hardware specific controller state structure
  331. */
  332. static void check_pending(struct bas_cardstate *ucs)
  333. {
  334. unsigned long flags;
  335. spin_lock_irqsave(&ucs->lock, flags);
  336. switch (ucs->pending) {
  337. case 0:
  338. break;
  339. case HD_OPEN_ATCHANNEL:
  340. if (ucs->basstate & BS_ATOPEN)
  341. ucs->pending = 0;
  342. break;
  343. case HD_OPEN_B1CHANNEL:
  344. if (ucs->basstate & BS_B1OPEN)
  345. ucs->pending = 0;
  346. break;
  347. case HD_OPEN_B2CHANNEL:
  348. if (ucs->basstate & BS_B2OPEN)
  349. ucs->pending = 0;
  350. break;
  351. case HD_CLOSE_ATCHANNEL:
  352. if (!(ucs->basstate & BS_ATOPEN))
  353. ucs->pending = 0;
  354. break;
  355. case HD_CLOSE_B1CHANNEL:
  356. if (!(ucs->basstate & BS_B1OPEN))
  357. ucs->pending = 0;
  358. break;
  359. case HD_CLOSE_B2CHANNEL:
  360. if (!(ucs->basstate & BS_B2OPEN))
  361. ucs->pending = 0;
  362. break;
  363. case HD_DEVICE_INIT_ACK: /* no reply expected */
  364. ucs->pending = 0;
  365. break;
  366. case HD_RESET_INTERRUPT_PIPE:
  367. if (!(ucs->basstate & BS_RESETTING))
  368. ucs->pending = 0;
  369. break;
  370. /*
  371. * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
  372. * and should never end up here
  373. */
  374. default:
  375. dev_warn(&ucs->interface->dev,
  376. "unknown pending request 0x%02x cleared\n",
  377. ucs->pending);
  378. ucs->pending = 0;
  379. }
  380. if (!ucs->pending)
  381. del_timer(&ucs->timer_ctrl);
  382. spin_unlock_irqrestore(&ucs->lock, flags);
  383. }
  384. /* cmd_in_timeout
  385. * timeout routine for command input request
  386. * argument:
  387. * controller state structure
  388. */
  389. static void cmd_in_timeout(unsigned long data)
  390. {
  391. struct cardstate *cs = (struct cardstate *) data;
  392. struct bas_cardstate *ucs = cs->hw.bas;
  393. int rc;
  394. if (!ucs->rcvbuf_size) {
  395. gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
  396. return;
  397. }
  398. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  399. dev_notice(cs->dev, "control read: timeout, retry %d\n",
  400. ucs->retry_cmd_in);
  401. rc = atread_submit(cs, BAS_TIMEOUT);
  402. if (rc >= 0 || rc == -ENODEV)
  403. /* resubmitted or disconnected */
  404. /* - bypass regular exit block */
  405. return;
  406. } else {
  407. dev_err(cs->dev,
  408. "control read: timeout, giving up after %d tries\n",
  409. ucs->retry_cmd_in);
  410. }
  411. kfree(ucs->rcvbuf);
  412. ucs->rcvbuf = NULL;
  413. ucs->rcvbuf_size = 0;
  414. error_reset(cs);
  415. }
  416. /* read_ctrl_callback
  417. * USB completion handler for control pipe input
  418. * called by the USB subsystem in interrupt context
  419. * parameter:
  420. * urb USB request block
  421. * urb->context = inbuf structure for controller state
  422. */
  423. static void read_ctrl_callback(struct urb *urb)
  424. {
  425. struct inbuf_t *inbuf = urb->context;
  426. struct cardstate *cs = inbuf->cs;
  427. struct bas_cardstate *ucs = cs->hw.bas;
  428. int status = urb->status;
  429. int have_data = 0;
  430. unsigned numbytes;
  431. int rc;
  432. update_basstate(ucs, 0, BS_ATRDPEND);
  433. wake_up(&ucs->waitqueue);
  434. if (!ucs->rcvbuf_size) {
  435. dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
  436. return;
  437. }
  438. del_timer(&ucs->timer_cmd_in);
  439. switch (status) {
  440. case 0: /* normal completion */
  441. numbytes = urb->actual_length;
  442. if (unlikely(numbytes != ucs->rcvbuf_size)) {
  443. dev_warn(cs->dev,
  444. "control read: received %d chars, expected %d\n",
  445. numbytes, ucs->rcvbuf_size);
  446. if (numbytes > ucs->rcvbuf_size)
  447. numbytes = ucs->rcvbuf_size;
  448. }
  449. /* copy received bytes to inbuf */
  450. have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
  451. if (unlikely(numbytes < ucs->rcvbuf_size)) {
  452. /* incomplete - resubmit for remaining bytes */
  453. ucs->rcvbuf_size -= numbytes;
  454. ucs->retry_cmd_in = 0;
  455. rc = atread_submit(cs, BAS_TIMEOUT);
  456. if (rc >= 0 || rc == -ENODEV)
  457. /* resubmitted or disconnected */
  458. /* - bypass regular exit block */
  459. return;
  460. error_reset(cs);
  461. }
  462. break;
  463. case -ENOENT: /* cancelled */
  464. case -ECONNRESET: /* cancelled (async) */
  465. case -EINPROGRESS: /* pending */
  466. case -ENODEV: /* device removed */
  467. case -ESHUTDOWN: /* device shut down */
  468. /* no action necessary */
  469. gig_dbg(DEBUG_USBREQ, "%s: %s",
  470. __func__, get_usb_statmsg(status));
  471. break;
  472. default: /* severe trouble */
  473. dev_warn(cs->dev, "control read: %s\n",
  474. get_usb_statmsg(status));
  475. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  476. dev_notice(cs->dev, "control read: retry %d\n",
  477. ucs->retry_cmd_in);
  478. rc = atread_submit(cs, BAS_TIMEOUT);
  479. if (rc >= 0 || rc == -ENODEV)
  480. /* resubmitted or disconnected */
  481. /* - bypass regular exit block */
  482. return;
  483. } else {
  484. dev_err(cs->dev,
  485. "control read: giving up after %d tries\n",
  486. ucs->retry_cmd_in);
  487. }
  488. error_reset(cs);
  489. }
  490. kfree(ucs->rcvbuf);
  491. ucs->rcvbuf = NULL;
  492. ucs->rcvbuf_size = 0;
  493. if (have_data) {
  494. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  495. gigaset_schedule_event(cs);
  496. }
  497. }
  498. /* atread_submit
  499. * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
  500. * parameters:
  501. * cs controller state structure
  502. * timeout timeout in 1/10 sec., 0: none
  503. * return value:
  504. * 0 on success
  505. * -EBUSY if another request is pending
  506. * any URB submission error code
  507. */
  508. static int atread_submit(struct cardstate *cs, int timeout)
  509. {
  510. struct bas_cardstate *ucs = cs->hw.bas;
  511. int basstate;
  512. int ret;
  513. gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
  514. ucs->rcvbuf_size);
  515. basstate = update_basstate(ucs, BS_ATRDPEND, 0);
  516. if (basstate & BS_ATRDPEND) {
  517. dev_err(cs->dev,
  518. "could not submit HD_READ_ATMESSAGE: URB busy\n");
  519. return -EBUSY;
  520. }
  521. if (basstate & BS_SUSPEND) {
  522. dev_notice(cs->dev,
  523. "HD_READ_ATMESSAGE not submitted, "
  524. "suspend in progress\n");
  525. update_basstate(ucs, 0, BS_ATRDPEND);
  526. /* treat like disconnect */
  527. return -ENODEV;
  528. }
  529. ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
  530. ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
  531. ucs->dr_cmd_in.wValue = 0;
  532. ucs->dr_cmd_in.wIndex = 0;
  533. ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
  534. usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
  535. usb_rcvctrlpipe(ucs->udev, 0),
  536. (unsigned char*) & ucs->dr_cmd_in,
  537. ucs->rcvbuf, ucs->rcvbuf_size,
  538. read_ctrl_callback, cs->inbuf);
  539. if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
  540. update_basstate(ucs, 0, BS_ATRDPEND);
  541. dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
  542. get_usb_rcmsg(ret));
  543. return ret;
  544. }
  545. if (timeout > 0) {
  546. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  547. ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
  548. ucs->timer_cmd_in.data = (unsigned long) cs;
  549. ucs->timer_cmd_in.function = cmd_in_timeout;
  550. add_timer(&ucs->timer_cmd_in);
  551. }
  552. return 0;
  553. }
  554. /* read_int_callback
  555. * USB completion handler for interrupt pipe input
  556. * called by the USB subsystem in interrupt context
  557. * parameter:
  558. * urb USB request block
  559. * urb->context = controller state structure
  560. */
  561. static void read_int_callback(struct urb *urb)
  562. {
  563. struct cardstate *cs = urb->context;
  564. struct bas_cardstate *ucs = cs->hw.bas;
  565. struct bc_state *bcs;
  566. int status = urb->status;
  567. unsigned long flags;
  568. int rc;
  569. unsigned l;
  570. int channel;
  571. switch (status) {
  572. case 0: /* success */
  573. break;
  574. case -ENOENT: /* cancelled */
  575. case -ECONNRESET: /* cancelled (async) */
  576. case -EINPROGRESS: /* pending */
  577. /* ignore silently */
  578. gig_dbg(DEBUG_USBREQ, "%s: %s",
  579. __func__, get_usb_statmsg(status));
  580. return;
  581. case -ENODEV: /* device removed */
  582. case -ESHUTDOWN: /* device shut down */
  583. //FIXME use this as disconnect indicator?
  584. gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
  585. return;
  586. default: /* severe trouble */
  587. dev_warn(cs->dev, "interrupt read: %s\n",
  588. get_usb_statmsg(status));
  589. //FIXME corrective action? resubmission always ok?
  590. goto resubmit;
  591. }
  592. /* drop incomplete packets even if the missing bytes wouldn't matter */
  593. if (unlikely(urb->actual_length < IP_MSGSIZE)) {
  594. dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
  595. urb->actual_length);
  596. goto resubmit;
  597. }
  598. l = (unsigned) ucs->int_in_buf[1] +
  599. (((unsigned) ucs->int_in_buf[2]) << 8);
  600. gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
  601. urb->actual_length, (int)ucs->int_in_buf[0], l,
  602. (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
  603. channel = 0;
  604. switch (ucs->int_in_buf[0]) {
  605. case HD_DEVICE_INIT_OK:
  606. update_basstate(ucs, BS_INIT, 0);
  607. break;
  608. case HD_READY_SEND_ATDATA:
  609. del_timer(&ucs->timer_atrdy);
  610. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  611. start_cbsend(cs);
  612. break;
  613. case HD_OPEN_B2CHANNEL_ACK:
  614. ++channel;
  615. case HD_OPEN_B1CHANNEL_ACK:
  616. bcs = cs->bcs + channel;
  617. update_basstate(ucs, BS_B1OPEN << channel, 0);
  618. gigaset_bchannel_up(bcs);
  619. break;
  620. case HD_OPEN_ATCHANNEL_ACK:
  621. update_basstate(ucs, BS_ATOPEN, 0);
  622. start_cbsend(cs);
  623. break;
  624. case HD_CLOSE_B2CHANNEL_ACK:
  625. ++channel;
  626. case HD_CLOSE_B1CHANNEL_ACK:
  627. bcs = cs->bcs + channel;
  628. update_basstate(ucs, 0, BS_B1OPEN << channel);
  629. stopurbs(bcs->hw.bas);
  630. gigaset_bchannel_down(bcs);
  631. break;
  632. case HD_CLOSE_ATCHANNEL_ACK:
  633. update_basstate(ucs, 0, BS_ATOPEN);
  634. break;
  635. case HD_B2_FLOW_CONTROL:
  636. ++channel;
  637. case HD_B1_FLOW_CONTROL:
  638. bcs = cs->bcs + channel;
  639. atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
  640. &bcs->hw.bas->corrbytes);
  641. gig_dbg(DEBUG_ISO,
  642. "Flow control (channel %d, sub %d): 0x%02x => %d",
  643. channel, bcs->hw.bas->numsub, l,
  644. atomic_read(&bcs->hw.bas->corrbytes));
  645. break;
  646. case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
  647. if (!l) {
  648. dev_warn(cs->dev,
  649. "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
  650. break;
  651. }
  652. spin_lock_irqsave(&cs->lock, flags);
  653. if (ucs->rcvbuf_size) {
  654. /* throw away previous buffer - we have no queue */
  655. dev_err(cs->dev,
  656. "receive AT data overrun, %d bytes lost\n",
  657. ucs->rcvbuf_size);
  658. kfree(ucs->rcvbuf);
  659. ucs->rcvbuf_size = 0;
  660. }
  661. if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
  662. spin_unlock_irqrestore(&cs->lock, flags);
  663. dev_err(cs->dev, "out of memory receiving AT data\n");
  664. error_reset(cs);
  665. break;
  666. }
  667. ucs->rcvbuf_size = l;
  668. ucs->retry_cmd_in = 0;
  669. if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
  670. kfree(ucs->rcvbuf);
  671. ucs->rcvbuf = NULL;
  672. ucs->rcvbuf_size = 0;
  673. if (rc != -ENODEV) {
  674. //FIXME corrective action?
  675. spin_unlock_irqrestore(&cs->lock, flags);
  676. error_reset(cs);
  677. break;
  678. }
  679. }
  680. spin_unlock_irqrestore(&cs->lock, flags);
  681. break;
  682. case HD_RESET_INTERRUPT_PIPE_ACK:
  683. update_basstate(ucs, 0, BS_RESETTING);
  684. dev_notice(cs->dev, "interrupt pipe reset\n");
  685. break;
  686. case HD_SUSPEND_END:
  687. gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
  688. break;
  689. default:
  690. dev_warn(cs->dev,
  691. "unknown Gigaset signal 0x%02x (%u) ignored\n",
  692. (int) ucs->int_in_buf[0], l);
  693. }
  694. check_pending(ucs);
  695. wake_up(&ucs->waitqueue);
  696. resubmit:
  697. rc = usb_submit_urb(urb, GFP_ATOMIC);
  698. if (unlikely(rc != 0 && rc != -ENODEV)) {
  699. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  700. get_usb_rcmsg(rc));
  701. error_reset(cs);
  702. }
  703. }
  704. /* read_iso_callback
  705. * USB completion handler for B channel isochronous input
  706. * called by the USB subsystem in interrupt context
  707. * parameter:
  708. * urb USB request block of completed request
  709. * urb->context = bc_state structure
  710. */
  711. static void read_iso_callback(struct urb *urb)
  712. {
  713. struct bc_state *bcs;
  714. struct bas_bc_state *ubc;
  715. int status = urb->status;
  716. unsigned long flags;
  717. int i, rc;
  718. /* status codes not worth bothering the tasklet with */
  719. if (unlikely(status == -ENOENT ||
  720. status == -ECONNRESET ||
  721. status == -EINPROGRESS ||
  722. status == -ENODEV ||
  723. status == -ESHUTDOWN)) {
  724. gig_dbg(DEBUG_ISO, "%s: %s",
  725. __func__, get_usb_statmsg(status));
  726. return;
  727. }
  728. bcs = urb->context;
  729. ubc = bcs->hw.bas;
  730. spin_lock_irqsave(&ubc->isoinlock, flags);
  731. if (likely(ubc->isoindone == NULL)) {
  732. /* pass URB to tasklet */
  733. ubc->isoindone = urb;
  734. ubc->isoinstatus = status;
  735. tasklet_hi_schedule(&ubc->rcvd_tasklet);
  736. } else {
  737. /* tasklet still busy, drop data and resubmit URB */
  738. ubc->loststatus = status;
  739. for (i = 0; i < BAS_NUMFRAMES; i++) {
  740. ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
  741. if (unlikely(urb->iso_frame_desc[i].status != 0 &&
  742. urb->iso_frame_desc[i].status !=
  743. -EINPROGRESS))
  744. ubc->loststatus = urb->iso_frame_desc[i].status;
  745. urb->iso_frame_desc[i].status = 0;
  746. urb->iso_frame_desc[i].actual_length = 0;
  747. }
  748. if (likely(ubc->running)) {
  749. /* urb->dev is clobbered by USB subsystem */
  750. urb->dev = bcs->cs->hw.bas->udev;
  751. urb->transfer_flags = URB_ISO_ASAP;
  752. urb->number_of_packets = BAS_NUMFRAMES;
  753. gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
  754. __func__);
  755. rc = usb_submit_urb(urb, GFP_ATOMIC);
  756. if (unlikely(rc != 0 && rc != -ENODEV)) {
  757. dev_err(bcs->cs->dev,
  758. "could not resubmit isochronous read "
  759. "URB: %s\n", get_usb_rcmsg(rc));
  760. dump_urb(DEBUG_ISO, "isoc read", urb);
  761. error_hangup(bcs);
  762. }
  763. }
  764. }
  765. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  766. }
  767. /* write_iso_callback
  768. * USB completion handler for B channel isochronous output
  769. * called by the USB subsystem in interrupt context
  770. * parameter:
  771. * urb USB request block of completed request
  772. * urb->context = isow_urbctx_t structure
  773. */
  774. static void write_iso_callback(struct urb *urb)
  775. {
  776. struct isow_urbctx_t *ucx;
  777. struct bas_bc_state *ubc;
  778. int status = urb->status;
  779. unsigned long flags;
  780. /* status codes not worth bothering the tasklet with */
  781. if (unlikely(status == -ENOENT ||
  782. status == -ECONNRESET ||
  783. status == -EINPROGRESS ||
  784. status == -ENODEV ||
  785. status == -ESHUTDOWN)) {
  786. gig_dbg(DEBUG_ISO, "%s: %s",
  787. __func__, get_usb_statmsg(status));
  788. return;
  789. }
  790. /* pass URB context to tasklet */
  791. ucx = urb->context;
  792. ubc = ucx->bcs->hw.bas;
  793. ucx->status = status;
  794. spin_lock_irqsave(&ubc->isooutlock, flags);
  795. ubc->isooutovfl = ubc->isooutdone;
  796. ubc->isooutdone = ucx;
  797. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  798. tasklet_hi_schedule(&ubc->sent_tasklet);
  799. }
  800. /* starturbs
  801. * prepare and submit USB request blocks for isochronous input and output
  802. * argument:
  803. * B channel control structure
  804. * return value:
  805. * 0 on success
  806. * < 0 on error (no URBs submitted)
  807. */
  808. static int starturbs(struct bc_state *bcs)
  809. {
  810. struct bas_bc_state *ubc = bcs->hw.bas;
  811. struct urb *urb;
  812. int j, k;
  813. int rc;
  814. /* initialize L2 reception */
  815. if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
  816. bcs->inputstate |= INS_flag_hunt;
  817. /* submit all isochronous input URBs */
  818. ubc->running = 1;
  819. for (k = 0; k < BAS_INURBS; k++) {
  820. urb = ubc->isoinurbs[k];
  821. if (!urb) {
  822. rc = -EFAULT;
  823. goto error;
  824. }
  825. urb->dev = bcs->cs->hw.bas->udev;
  826. urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
  827. urb->transfer_flags = URB_ISO_ASAP;
  828. urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
  829. urb->transfer_buffer_length = BAS_INBUFSIZE;
  830. urb->number_of_packets = BAS_NUMFRAMES;
  831. urb->interval = BAS_FRAMETIME;
  832. urb->complete = read_iso_callback;
  833. urb->context = bcs;
  834. for (j = 0; j < BAS_NUMFRAMES; j++) {
  835. urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
  836. urb->iso_frame_desc[j].length = BAS_MAXFRAME;
  837. urb->iso_frame_desc[j].status = 0;
  838. urb->iso_frame_desc[j].actual_length = 0;
  839. }
  840. dump_urb(DEBUG_ISO, "Initial isoc read", urb);
  841. if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
  842. goto error;
  843. }
  844. /* initialize L2 transmission */
  845. gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
  846. /* set up isochronous output URBs for flag idling */
  847. for (k = 0; k < BAS_OUTURBS; ++k) {
  848. urb = ubc->isoouturbs[k].urb;
  849. if (!urb) {
  850. rc = -EFAULT;
  851. goto error;
  852. }
  853. urb->dev = bcs->cs->hw.bas->udev;
  854. urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
  855. urb->transfer_flags = URB_ISO_ASAP;
  856. urb->transfer_buffer = ubc->isooutbuf->data;
  857. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  858. urb->number_of_packets = BAS_NUMFRAMES;
  859. urb->interval = BAS_FRAMETIME;
  860. urb->complete = write_iso_callback;
  861. urb->context = &ubc->isoouturbs[k];
  862. for (j = 0; j < BAS_NUMFRAMES; ++j) {
  863. urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
  864. urb->iso_frame_desc[j].length = BAS_NORMFRAME;
  865. urb->iso_frame_desc[j].status = 0;
  866. urb->iso_frame_desc[j].actual_length = 0;
  867. }
  868. ubc->isoouturbs[k].limit = -1;
  869. }
  870. /* keep one URB free, submit the others */
  871. for (k = 0; k < BAS_OUTURBS-1; ++k) {
  872. dump_urb(DEBUG_ISO, "Initial isoc write", urb);
  873. rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
  874. if (rc != 0)
  875. goto error;
  876. }
  877. dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
  878. ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
  879. ubc->isooutdone = ubc->isooutovfl = NULL;
  880. return 0;
  881. error:
  882. stopurbs(ubc);
  883. return rc;
  884. }
  885. /* stopurbs
  886. * cancel the USB request blocks for isochronous input and output
  887. * errors are silently ignored
  888. * argument:
  889. * B channel control structure
  890. */
  891. static void stopurbs(struct bas_bc_state *ubc)
  892. {
  893. int k, rc;
  894. ubc->running = 0;
  895. for (k = 0; k < BAS_INURBS; ++k) {
  896. rc = usb_unlink_urb(ubc->isoinurbs[k]);
  897. gig_dbg(DEBUG_ISO,
  898. "%s: isoc input URB %d unlinked, result = %s",
  899. __func__, k, get_usb_rcmsg(rc));
  900. }
  901. for (k = 0; k < BAS_OUTURBS; ++k) {
  902. rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
  903. gig_dbg(DEBUG_ISO,
  904. "%s: isoc output URB %d unlinked, result = %s",
  905. __func__, k, get_usb_rcmsg(rc));
  906. }
  907. }
  908. /* Isochronous Write - Bottom Half */
  909. /* =============================== */
  910. /* submit_iso_write_urb
  911. * fill and submit the next isochronous write URB
  912. * parameters:
  913. * ucx context structure containing URB
  914. * return value:
  915. * number of frames submitted in URB
  916. * 0 if URB not submitted because no data available (isooutbuf busy)
  917. * error code < 0 on error
  918. */
  919. static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
  920. {
  921. struct urb *urb = ucx->urb;
  922. struct bas_bc_state *ubc = ucx->bcs->hw.bas;
  923. struct usb_iso_packet_descriptor *ifd;
  924. int corrbytes, nframe, rc;
  925. /* urb->dev is clobbered by USB subsystem */
  926. urb->dev = ucx->bcs->cs->hw.bas->udev;
  927. urb->transfer_flags = URB_ISO_ASAP;
  928. urb->transfer_buffer = ubc->isooutbuf->data;
  929. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  930. for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
  931. ifd = &urb->iso_frame_desc[nframe];
  932. /* compute frame length according to flow control */
  933. ifd->length = BAS_NORMFRAME;
  934. if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
  935. gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
  936. __func__, corrbytes);
  937. if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
  938. corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
  939. else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
  940. corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
  941. ifd->length += corrbytes;
  942. atomic_add(-corrbytes, &ubc->corrbytes);
  943. }
  944. /* retrieve block of data to send */
  945. rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
  946. if (rc < 0) {
  947. if (rc == -EBUSY) {
  948. gig_dbg(DEBUG_ISO,
  949. "%s: buffer busy at frame %d",
  950. __func__, nframe);
  951. /* tasklet will be restarted from
  952. gigaset_send_skb() */
  953. } else {
  954. dev_err(ucx->bcs->cs->dev,
  955. "%s: buffer error %d at frame %d\n",
  956. __func__, rc, nframe);
  957. return rc;
  958. }
  959. break;
  960. }
  961. ifd->offset = rc;
  962. ucx->limit = ubc->isooutbuf->nextread;
  963. ifd->status = 0;
  964. ifd->actual_length = 0;
  965. }
  966. if (unlikely(nframe == 0))
  967. return 0; /* no data to send */
  968. urb->number_of_packets = nframe;
  969. rc = usb_submit_urb(urb, GFP_ATOMIC);
  970. if (unlikely(rc)) {
  971. if (rc == -ENODEV)
  972. /* device removed - give up silently */
  973. gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
  974. else
  975. dev_err(ucx->bcs->cs->dev,
  976. "could not submit isochronous write URB: %s\n",
  977. get_usb_rcmsg(rc));
  978. return rc;
  979. }
  980. ++ubc->numsub;
  981. return nframe;
  982. }
  983. /* write_iso_tasklet
  984. * tasklet scheduled when an isochronous output URB from the Gigaset device
  985. * has completed
  986. * parameter:
  987. * data B channel state structure
  988. */
  989. static void write_iso_tasklet(unsigned long data)
  990. {
  991. struct bc_state *bcs = (struct bc_state *) data;
  992. struct bas_bc_state *ubc = bcs->hw.bas;
  993. struct cardstate *cs = bcs->cs;
  994. struct isow_urbctx_t *done, *next, *ovfl;
  995. struct urb *urb;
  996. int status;
  997. struct usb_iso_packet_descriptor *ifd;
  998. int offset;
  999. unsigned long flags;
  1000. int i;
  1001. struct sk_buff *skb;
  1002. int len;
  1003. int rc;
  1004. /* loop while completed URBs arrive in time */
  1005. for (;;) {
  1006. if (unlikely(!(ubc->running))) {
  1007. gig_dbg(DEBUG_ISO, "%s: not running", __func__);
  1008. return;
  1009. }
  1010. /* retrieve completed URBs */
  1011. spin_lock_irqsave(&ubc->isooutlock, flags);
  1012. done = ubc->isooutdone;
  1013. ubc->isooutdone = NULL;
  1014. ovfl = ubc->isooutovfl;
  1015. ubc->isooutovfl = NULL;
  1016. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1017. if (ovfl) {
  1018. dev_err(cs->dev, "isochronous write buffer underrun\n");
  1019. error_hangup(bcs);
  1020. break;
  1021. }
  1022. if (!done)
  1023. break;
  1024. /* submit free URB if available */
  1025. spin_lock_irqsave(&ubc->isooutlock, flags);
  1026. next = ubc->isooutfree;
  1027. ubc->isooutfree = NULL;
  1028. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1029. if (next) {
  1030. rc = submit_iso_write_urb(next);
  1031. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1032. /* could not submit URB, put it back */
  1033. spin_lock_irqsave(&ubc->isooutlock, flags);
  1034. if (ubc->isooutfree == NULL) {
  1035. ubc->isooutfree = next;
  1036. next = NULL;
  1037. }
  1038. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1039. if (next) {
  1040. /* couldn't put it back */
  1041. dev_err(cs->dev,
  1042. "losing isochronous write URB\n");
  1043. error_hangup(bcs);
  1044. }
  1045. }
  1046. }
  1047. /* process completed URB */
  1048. urb = done->urb;
  1049. status = done->status;
  1050. switch (status) {
  1051. case -EXDEV: /* partial completion */
  1052. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1053. __func__);
  1054. /* fall through - what's the difference anyway? */
  1055. case 0: /* normal completion */
  1056. /* inspect individual frames
  1057. * assumptions (for lack of documentation):
  1058. * - actual_length bytes of first frame in error are
  1059. * successfully sent
  1060. * - all following frames are not sent at all
  1061. */
  1062. offset = done->limit; /* default (no error) */
  1063. for (i = 0; i < BAS_NUMFRAMES; i++) {
  1064. ifd = &urb->iso_frame_desc[i];
  1065. if (ifd->status ||
  1066. ifd->actual_length != ifd->length) {
  1067. dev_warn(cs->dev,
  1068. "isochronous write: frame %d: %s, "
  1069. "only %d of %d bytes sent\n",
  1070. i, get_usb_statmsg(ifd->status),
  1071. ifd->actual_length, ifd->length);
  1072. offset = (ifd->offset +
  1073. ifd->actual_length)
  1074. % BAS_OUTBUFSIZE;
  1075. break;
  1076. }
  1077. }
  1078. #ifdef CONFIG_GIGASET_DEBUG
  1079. /* check assumption on remaining frames */
  1080. for (; i < BAS_NUMFRAMES; i++) {
  1081. ifd = &urb->iso_frame_desc[i];
  1082. if (ifd->status != -EINPROGRESS
  1083. || ifd->actual_length != 0) {
  1084. dev_warn(cs->dev,
  1085. "isochronous write: frame %d: %s, "
  1086. "%d of %d bytes sent\n",
  1087. i, get_usb_statmsg(ifd->status),
  1088. ifd->actual_length, ifd->length);
  1089. offset = (ifd->offset +
  1090. ifd->actual_length)
  1091. % BAS_OUTBUFSIZE;
  1092. break;
  1093. }
  1094. }
  1095. #endif
  1096. break;
  1097. case -EPIPE: /* stall - probably underrun */
  1098. dev_err(cs->dev, "isochronous write stalled\n");
  1099. error_hangup(bcs);
  1100. break;
  1101. default: /* severe trouble */
  1102. dev_warn(cs->dev, "isochronous write: %s\n",
  1103. get_usb_statmsg(status));
  1104. }
  1105. /* mark the write buffer area covered by this URB as free */
  1106. if (done->limit >= 0)
  1107. ubc->isooutbuf->read = done->limit;
  1108. /* mark URB as free */
  1109. spin_lock_irqsave(&ubc->isooutlock, flags);
  1110. next = ubc->isooutfree;
  1111. ubc->isooutfree = done;
  1112. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1113. if (next) {
  1114. /* only one URB still active - resubmit one */
  1115. rc = submit_iso_write_urb(next);
  1116. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1117. /* couldn't submit */
  1118. error_hangup(bcs);
  1119. }
  1120. }
  1121. }
  1122. /* process queued SKBs */
  1123. while ((skb = skb_dequeue(&bcs->squeue))) {
  1124. /* copy to output buffer, doing L2 encapsulation */
  1125. len = skb->len;
  1126. if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
  1127. /* insufficient buffer space, push back onto queue */
  1128. skb_queue_head(&bcs->squeue, skb);
  1129. gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
  1130. __func__, skb_queue_len(&bcs->squeue));
  1131. break;
  1132. }
  1133. skb_pull(skb, len);
  1134. gigaset_skb_sent(bcs, skb);
  1135. dev_kfree_skb_any(skb);
  1136. }
  1137. }
  1138. /* Isochronous Read - Bottom Half */
  1139. /* ============================== */
  1140. /* read_iso_tasklet
  1141. * tasklet scheduled when an isochronous input URB from the Gigaset device
  1142. * has completed
  1143. * parameter:
  1144. * data B channel state structure
  1145. */
  1146. static void read_iso_tasklet(unsigned long data)
  1147. {
  1148. struct bc_state *bcs = (struct bc_state *) data;
  1149. struct bas_bc_state *ubc = bcs->hw.bas;
  1150. struct cardstate *cs = bcs->cs;
  1151. struct urb *urb;
  1152. int status;
  1153. char *rcvbuf;
  1154. unsigned long flags;
  1155. int totleft, numbytes, offset, frame, rc;
  1156. /* loop while more completed URBs arrive in the meantime */
  1157. for (;;) {
  1158. /* retrieve URB */
  1159. spin_lock_irqsave(&ubc->isoinlock, flags);
  1160. if (!(urb = ubc->isoindone)) {
  1161. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1162. return;
  1163. }
  1164. status = ubc->isoinstatus;
  1165. ubc->isoindone = NULL;
  1166. if (unlikely(ubc->loststatus != -EINPROGRESS)) {
  1167. dev_warn(cs->dev,
  1168. "isochronous read overrun, "
  1169. "dropped URB with status: %s, %d bytes lost\n",
  1170. get_usb_statmsg(ubc->loststatus),
  1171. ubc->isoinlost);
  1172. ubc->loststatus = -EINPROGRESS;
  1173. }
  1174. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1175. if (unlikely(!(ubc->running))) {
  1176. gig_dbg(DEBUG_ISO,
  1177. "%s: channel not running, "
  1178. "dropped URB with status: %s",
  1179. __func__, get_usb_statmsg(status));
  1180. return;
  1181. }
  1182. switch (status) {
  1183. case 0: /* normal completion */
  1184. break;
  1185. case -EXDEV: /* inspect individual frames
  1186. (we do that anyway) */
  1187. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1188. __func__);
  1189. break;
  1190. case -ENOENT:
  1191. case -ECONNRESET:
  1192. case -EINPROGRESS:
  1193. gig_dbg(DEBUG_ISO, "%s: %s",
  1194. __func__, get_usb_statmsg(status));
  1195. continue; /* -> skip */
  1196. case -EPIPE:
  1197. dev_err(cs->dev, "isochronous read stalled\n");
  1198. error_hangup(bcs);
  1199. continue; /* -> skip */
  1200. default: /* severe trouble */
  1201. dev_warn(cs->dev, "isochronous read: %s\n",
  1202. get_usb_statmsg(status));
  1203. goto error;
  1204. }
  1205. rcvbuf = urb->transfer_buffer;
  1206. totleft = urb->actual_length;
  1207. for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
  1208. numbytes = urb->iso_frame_desc[frame].actual_length;
  1209. if (unlikely(urb->iso_frame_desc[frame].status))
  1210. dev_warn(cs->dev,
  1211. "isochronous read: frame %d[%d]: %s\n",
  1212. frame, numbytes,
  1213. get_usb_statmsg(
  1214. urb->iso_frame_desc[frame].status));
  1215. if (unlikely(numbytes > BAS_MAXFRAME))
  1216. dev_warn(cs->dev,
  1217. "isochronous read: frame %d: "
  1218. "numbytes (%d) > BAS_MAXFRAME\n",
  1219. frame, numbytes);
  1220. if (unlikely(numbytes > totleft)) {
  1221. dev_warn(cs->dev,
  1222. "isochronous read: frame %d: "
  1223. "numbytes (%d) > totleft (%d)\n",
  1224. frame, numbytes, totleft);
  1225. numbytes = totleft;
  1226. }
  1227. offset = urb->iso_frame_desc[frame].offset;
  1228. if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
  1229. dev_warn(cs->dev,
  1230. "isochronous read: frame %d: "
  1231. "offset (%d) + numbytes (%d) "
  1232. "> BAS_INBUFSIZE\n",
  1233. frame, offset, numbytes);
  1234. numbytes = BAS_INBUFSIZE - offset;
  1235. }
  1236. gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
  1237. totleft -= numbytes;
  1238. }
  1239. if (unlikely(totleft > 0))
  1240. dev_warn(cs->dev,
  1241. "isochronous read: %d data bytes missing\n",
  1242. totleft);
  1243. error:
  1244. /* URB processed, resubmit */
  1245. for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
  1246. urb->iso_frame_desc[frame].status = 0;
  1247. urb->iso_frame_desc[frame].actual_length = 0;
  1248. }
  1249. /* urb->dev is clobbered by USB subsystem */
  1250. urb->dev = bcs->cs->hw.bas->udev;
  1251. urb->transfer_flags = URB_ISO_ASAP;
  1252. urb->number_of_packets = BAS_NUMFRAMES;
  1253. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1254. if (unlikely(rc != 0 && rc != -ENODEV)) {
  1255. dev_err(cs->dev,
  1256. "could not resubmit isochronous read URB: %s\n",
  1257. get_usb_rcmsg(rc));
  1258. dump_urb(DEBUG_ISO, "resubmit iso read", urb);
  1259. error_hangup(bcs);
  1260. }
  1261. }
  1262. }
  1263. /* Channel Operations */
  1264. /* ================== */
  1265. /* req_timeout
  1266. * timeout routine for control output request
  1267. * argument:
  1268. * B channel control structure
  1269. */
  1270. static void req_timeout(unsigned long data)
  1271. {
  1272. struct bc_state *bcs = (struct bc_state *) data;
  1273. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1274. int pending;
  1275. unsigned long flags;
  1276. check_pending(ucs);
  1277. spin_lock_irqsave(&ucs->lock, flags);
  1278. pending = ucs->pending;
  1279. ucs->pending = 0;
  1280. spin_unlock_irqrestore(&ucs->lock, flags);
  1281. switch (pending) {
  1282. case 0: /* no pending request */
  1283. gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
  1284. break;
  1285. case HD_OPEN_ATCHANNEL:
  1286. dev_err(bcs->cs->dev, "timeout opening AT channel\n");
  1287. error_reset(bcs->cs);
  1288. break;
  1289. case HD_OPEN_B2CHANNEL:
  1290. case HD_OPEN_B1CHANNEL:
  1291. dev_err(bcs->cs->dev, "timeout opening channel %d\n",
  1292. bcs->channel + 1);
  1293. error_hangup(bcs);
  1294. break;
  1295. case HD_CLOSE_ATCHANNEL:
  1296. dev_err(bcs->cs->dev, "timeout closing AT channel\n");
  1297. error_reset(bcs->cs);
  1298. break;
  1299. case HD_CLOSE_B2CHANNEL:
  1300. case HD_CLOSE_B1CHANNEL:
  1301. dev_err(bcs->cs->dev, "timeout closing channel %d\n",
  1302. bcs->channel + 1);
  1303. error_reset(bcs->cs);
  1304. break;
  1305. case HD_RESET_INTERRUPT_PIPE:
  1306. /* error recovery escalation */
  1307. dev_err(bcs->cs->dev,
  1308. "reset interrupt pipe timeout, attempting USB reset\n");
  1309. usb_queue_reset_device(bcs->cs->hw.bas->interface);
  1310. break;
  1311. default:
  1312. dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
  1313. pending);
  1314. }
  1315. wake_up(&ucs->waitqueue);
  1316. }
  1317. /* write_ctrl_callback
  1318. * USB completion handler for control pipe output
  1319. * called by the USB subsystem in interrupt context
  1320. * parameter:
  1321. * urb USB request block of completed request
  1322. * urb->context = hardware specific controller state structure
  1323. */
  1324. static void write_ctrl_callback(struct urb *urb)
  1325. {
  1326. struct bas_cardstate *ucs = urb->context;
  1327. int status = urb->status;
  1328. int rc;
  1329. unsigned long flags;
  1330. /* check status */
  1331. switch (status) {
  1332. case 0: /* normal completion */
  1333. spin_lock_irqsave(&ucs->lock, flags);
  1334. switch (ucs->pending) {
  1335. case HD_DEVICE_INIT_ACK: /* no reply expected */
  1336. del_timer(&ucs->timer_ctrl);
  1337. ucs->pending = 0;
  1338. break;
  1339. }
  1340. spin_unlock_irqrestore(&ucs->lock, flags);
  1341. return;
  1342. case -ENOENT: /* cancelled */
  1343. case -ECONNRESET: /* cancelled (async) */
  1344. case -EINPROGRESS: /* pending */
  1345. case -ENODEV: /* device removed */
  1346. case -ESHUTDOWN: /* device shut down */
  1347. /* ignore silently */
  1348. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1349. __func__, get_usb_statmsg(status));
  1350. break;
  1351. default: /* any failure */
  1352. /* don't retry if suspend requested */
  1353. if (++ucs->retry_ctrl > BAS_RETRY ||
  1354. (ucs->basstate & BS_SUSPEND)) {
  1355. dev_err(&ucs->interface->dev,
  1356. "control request 0x%02x failed: %s\n",
  1357. ucs->dr_ctrl.bRequest,
  1358. get_usb_statmsg(status));
  1359. break; /* give up */
  1360. }
  1361. dev_notice(&ucs->interface->dev,
  1362. "control request 0x%02x: %s, retry %d\n",
  1363. ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
  1364. ucs->retry_ctrl);
  1365. /* urb->dev is clobbered by USB subsystem */
  1366. urb->dev = ucs->udev;
  1367. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1368. if (unlikely(rc)) {
  1369. dev_err(&ucs->interface->dev,
  1370. "could not resubmit request 0x%02x: %s\n",
  1371. ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
  1372. break;
  1373. }
  1374. /* resubmitted */
  1375. return;
  1376. }
  1377. /* failed, clear pending request */
  1378. spin_lock_irqsave(&ucs->lock, flags);
  1379. del_timer(&ucs->timer_ctrl);
  1380. ucs->pending = 0;
  1381. spin_unlock_irqrestore(&ucs->lock, flags);
  1382. wake_up(&ucs->waitqueue);
  1383. }
  1384. /* req_submit
  1385. * submit a control output request without message buffer to the Gigaset base
  1386. * and optionally start a timeout
  1387. * parameters:
  1388. * bcs B channel control structure
  1389. * req control request code (HD_*)
  1390. * val control request parameter value (set to 0 if unused)
  1391. * timeout timeout in seconds (0: no timeout)
  1392. * return value:
  1393. * 0 on success
  1394. * -EBUSY if another request is pending
  1395. * any URB submission error code
  1396. */
  1397. static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
  1398. {
  1399. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1400. int ret;
  1401. unsigned long flags;
  1402. gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
  1403. spin_lock_irqsave(&ucs->lock, flags);
  1404. if (ucs->pending) {
  1405. spin_unlock_irqrestore(&ucs->lock, flags);
  1406. dev_err(bcs->cs->dev,
  1407. "submission of request 0x%02x failed: "
  1408. "request 0x%02x still pending\n",
  1409. req, ucs->pending);
  1410. return -EBUSY;
  1411. }
  1412. ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
  1413. ucs->dr_ctrl.bRequest = req;
  1414. ucs->dr_ctrl.wValue = cpu_to_le16(val);
  1415. ucs->dr_ctrl.wIndex = 0;
  1416. ucs->dr_ctrl.wLength = 0;
  1417. usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
  1418. usb_sndctrlpipe(ucs->udev, 0),
  1419. (unsigned char*) &ucs->dr_ctrl, NULL, 0,
  1420. write_ctrl_callback, ucs);
  1421. ucs->retry_ctrl = 0;
  1422. ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
  1423. if (unlikely(ret)) {
  1424. dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
  1425. req, get_usb_rcmsg(ret));
  1426. spin_unlock_irqrestore(&ucs->lock, flags);
  1427. return ret;
  1428. }
  1429. ucs->pending = req;
  1430. if (timeout > 0) {
  1431. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  1432. ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
  1433. ucs->timer_ctrl.data = (unsigned long) bcs;
  1434. ucs->timer_ctrl.function = req_timeout;
  1435. add_timer(&ucs->timer_ctrl);
  1436. }
  1437. spin_unlock_irqrestore(&ucs->lock, flags);
  1438. return 0;
  1439. }
  1440. /* gigaset_init_bchannel
  1441. * called by common.c to connect a B channel
  1442. * initialize isochronous I/O and tell the Gigaset base to open the channel
  1443. * argument:
  1444. * B channel control structure
  1445. * return value:
  1446. * 0 on success, error code < 0 on error
  1447. */
  1448. static int gigaset_init_bchannel(struct bc_state *bcs)
  1449. {
  1450. struct cardstate *cs = bcs->cs;
  1451. int req, ret;
  1452. unsigned long flags;
  1453. spin_lock_irqsave(&cs->lock, flags);
  1454. if (unlikely(!cs->connected)) {
  1455. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1456. spin_unlock_irqrestore(&cs->lock, flags);
  1457. return -ENODEV;
  1458. }
  1459. if (cs->hw.bas->basstate & BS_SUSPEND) {
  1460. dev_notice(cs->dev,
  1461. "not starting isochronous I/O, "
  1462. "suspend in progress\n");
  1463. spin_unlock_irqrestore(&cs->lock, flags);
  1464. return -EHOSTUNREACH;
  1465. }
  1466. if ((ret = starturbs(bcs)) < 0) {
  1467. dev_err(cs->dev,
  1468. "could not start isochronous I/O for channel B%d: %s\n",
  1469. bcs->channel + 1,
  1470. ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
  1471. if (ret != -ENODEV)
  1472. error_hangup(bcs);
  1473. spin_unlock_irqrestore(&cs->lock, flags);
  1474. return ret;
  1475. }
  1476. req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
  1477. if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
  1478. dev_err(cs->dev, "could not open channel B%d\n",
  1479. bcs->channel + 1);
  1480. stopurbs(bcs->hw.bas);
  1481. if (ret != -ENODEV)
  1482. error_hangup(bcs);
  1483. }
  1484. spin_unlock_irqrestore(&cs->lock, flags);
  1485. return ret;
  1486. }
  1487. /* gigaset_close_bchannel
  1488. * called by common.c to disconnect a B channel
  1489. * tell the Gigaset base to close the channel
  1490. * stopping isochronous I/O and LL notification will be done when the
  1491. * acknowledgement for the close arrives
  1492. * argument:
  1493. * B channel control structure
  1494. * return value:
  1495. * 0 on success, error code < 0 on error
  1496. */
  1497. static int gigaset_close_bchannel(struct bc_state *bcs)
  1498. {
  1499. struct cardstate *cs = bcs->cs;
  1500. int req, ret;
  1501. unsigned long flags;
  1502. spin_lock_irqsave(&cs->lock, flags);
  1503. if (unlikely(!cs->connected)) {
  1504. spin_unlock_irqrestore(&cs->lock, flags);
  1505. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1506. return -ENODEV;
  1507. }
  1508. if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
  1509. /* channel not running: just signal common.c */
  1510. spin_unlock_irqrestore(&cs->lock, flags);
  1511. gigaset_bchannel_down(bcs);
  1512. return 0;
  1513. }
  1514. /* channel running: tell device to close it */
  1515. req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
  1516. if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
  1517. dev_err(cs->dev, "closing channel B%d failed\n",
  1518. bcs->channel + 1);
  1519. spin_unlock_irqrestore(&cs->lock, flags);
  1520. return ret;
  1521. }
  1522. /* Device Operations */
  1523. /* ================= */
  1524. /* complete_cb
  1525. * unqueue first command buffer from queue, waking any sleepers
  1526. * must be called with cs->cmdlock held
  1527. * parameter:
  1528. * cs controller state structure
  1529. */
  1530. static void complete_cb(struct cardstate *cs)
  1531. {
  1532. struct cmdbuf_t *cb = cs->cmdbuf;
  1533. /* unqueue completed buffer */
  1534. cs->cmdbytes -= cs->curlen;
  1535. gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
  1536. "write_command: sent %u bytes, %u left",
  1537. cs->curlen, cs->cmdbytes);
  1538. if ((cs->cmdbuf = cb->next) != NULL) {
  1539. cs->cmdbuf->prev = NULL;
  1540. cs->curlen = cs->cmdbuf->len;
  1541. } else {
  1542. cs->lastcmdbuf = NULL;
  1543. cs->curlen = 0;
  1544. }
  1545. if (cb->wake_tasklet)
  1546. tasklet_schedule(cb->wake_tasklet);
  1547. kfree(cb);
  1548. }
  1549. /* write_command_callback
  1550. * USB completion handler for AT command transmission
  1551. * called by the USB subsystem in interrupt context
  1552. * parameter:
  1553. * urb USB request block of completed request
  1554. * urb->context = controller state structure
  1555. */
  1556. static void write_command_callback(struct urb *urb)
  1557. {
  1558. struct cardstate *cs = urb->context;
  1559. struct bas_cardstate *ucs = cs->hw.bas;
  1560. int status = urb->status;
  1561. unsigned long flags;
  1562. update_basstate(ucs, 0, BS_ATWRPEND);
  1563. wake_up(&ucs->waitqueue);
  1564. /* check status */
  1565. switch (status) {
  1566. case 0: /* normal completion */
  1567. break;
  1568. case -ENOENT: /* cancelled */
  1569. case -ECONNRESET: /* cancelled (async) */
  1570. case -EINPROGRESS: /* pending */
  1571. case -ENODEV: /* device removed */
  1572. case -ESHUTDOWN: /* device shut down */
  1573. /* ignore silently */
  1574. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1575. __func__, get_usb_statmsg(status));
  1576. return;
  1577. default: /* any failure */
  1578. if (++ucs->retry_cmd_out > BAS_RETRY) {
  1579. dev_warn(cs->dev,
  1580. "command write: %s, "
  1581. "giving up after %d retries\n",
  1582. get_usb_statmsg(status),
  1583. ucs->retry_cmd_out);
  1584. break;
  1585. }
  1586. if (ucs->basstate & BS_SUSPEND) {
  1587. dev_warn(cs->dev,
  1588. "command write: %s, "
  1589. "won't retry - suspend requested\n",
  1590. get_usb_statmsg(status));
  1591. break;
  1592. }
  1593. if (cs->cmdbuf == NULL) {
  1594. dev_warn(cs->dev,
  1595. "command write: %s, "
  1596. "cannot retry - cmdbuf gone\n",
  1597. get_usb_statmsg(status));
  1598. break;
  1599. }
  1600. dev_notice(cs->dev, "command write: %s, retry %d\n",
  1601. get_usb_statmsg(status), ucs->retry_cmd_out);
  1602. if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
  1603. /* resubmitted - bypass regular exit block */
  1604. return;
  1605. /* command send failed, assume base still waiting */
  1606. update_basstate(ucs, BS_ATREADY, 0);
  1607. }
  1608. spin_lock_irqsave(&cs->cmdlock, flags);
  1609. if (cs->cmdbuf != NULL)
  1610. complete_cb(cs);
  1611. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1612. }
  1613. /* atrdy_timeout
  1614. * timeout routine for AT command transmission
  1615. * argument:
  1616. * controller state structure
  1617. */
  1618. static void atrdy_timeout(unsigned long data)
  1619. {
  1620. struct cardstate *cs = (struct cardstate *) data;
  1621. struct bas_cardstate *ucs = cs->hw.bas;
  1622. dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
  1623. /* fake the missing signal - what else can I do? */
  1624. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  1625. start_cbsend(cs);
  1626. }
  1627. /* atwrite_submit
  1628. * submit an HD_WRITE_ATMESSAGE command URB
  1629. * parameters:
  1630. * cs controller state structure
  1631. * buf buffer containing command to send
  1632. * len length of command to send
  1633. * return value:
  1634. * 0 on success
  1635. * -EBUSY if another request is pending
  1636. * any URB submission error code
  1637. */
  1638. static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
  1639. {
  1640. struct bas_cardstate *ucs = cs->hw.bas;
  1641. int rc;
  1642. gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
  1643. if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
  1644. dev_err(cs->dev,
  1645. "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
  1646. return -EBUSY;
  1647. }
  1648. ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
  1649. ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
  1650. ucs->dr_cmd_out.wValue = 0;
  1651. ucs->dr_cmd_out.wIndex = 0;
  1652. ucs->dr_cmd_out.wLength = cpu_to_le16(len);
  1653. usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
  1654. usb_sndctrlpipe(ucs->udev, 0),
  1655. (unsigned char*) &ucs->dr_cmd_out, buf, len,
  1656. write_command_callback, cs);
  1657. rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
  1658. if (unlikely(rc)) {
  1659. update_basstate(ucs, 0, BS_ATWRPEND);
  1660. dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
  1661. get_usb_rcmsg(rc));
  1662. return rc;
  1663. }
  1664. /* submitted successfully, start timeout if necessary */
  1665. if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
  1666. gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
  1667. ATRDY_TIMEOUT);
  1668. ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
  1669. ucs->timer_atrdy.data = (unsigned long) cs;
  1670. ucs->timer_atrdy.function = atrdy_timeout;
  1671. add_timer(&ucs->timer_atrdy);
  1672. }
  1673. return 0;
  1674. }
  1675. /* start_cbsend
  1676. * start transmission of AT command queue if necessary
  1677. * parameter:
  1678. * cs controller state structure
  1679. * return value:
  1680. * 0 on success
  1681. * error code < 0 on error
  1682. */
  1683. static int start_cbsend(struct cardstate *cs)
  1684. {
  1685. struct cmdbuf_t *cb;
  1686. struct bas_cardstate *ucs = cs->hw.bas;
  1687. unsigned long flags;
  1688. int rc;
  1689. int retval = 0;
  1690. /* check if suspend requested */
  1691. if (ucs->basstate & BS_SUSPEND) {
  1692. gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
  1693. return -EHOSTUNREACH;
  1694. }
  1695. /* check if AT channel is open */
  1696. if (!(ucs->basstate & BS_ATOPEN)) {
  1697. gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
  1698. rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
  1699. if (rc < 0) {
  1700. /* flush command queue */
  1701. spin_lock_irqsave(&cs->cmdlock, flags);
  1702. while (cs->cmdbuf != NULL)
  1703. complete_cb(cs);
  1704. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1705. }
  1706. return rc;
  1707. }
  1708. /* try to send first command in queue */
  1709. spin_lock_irqsave(&cs->cmdlock, flags);
  1710. while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
  1711. ucs->retry_cmd_out = 0;
  1712. rc = atwrite_submit(cs, cb->buf, cb->len);
  1713. if (unlikely(rc)) {
  1714. retval = rc;
  1715. complete_cb(cs);
  1716. }
  1717. }
  1718. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1719. return retval;
  1720. }
  1721. /* gigaset_write_cmd
  1722. * This function is called by the device independent part of the driver
  1723. * to transmit an AT command string to the Gigaset device.
  1724. * It encapsulates the device specific method for transmission over the
  1725. * direct USB connection to the base.
  1726. * The command string is added to the queue of commands to send, and
  1727. * USB transmission is started if necessary.
  1728. * parameters:
  1729. * cs controller state structure
  1730. * buf command string to send
  1731. * len number of bytes to send (max. IF_WRITEBUF)
  1732. * wake_tasklet tasklet to run when transmission is completed
  1733. * (NULL if none)
  1734. * return value:
  1735. * number of bytes queued on success
  1736. * error code < 0 on error
  1737. */
  1738. static int gigaset_write_cmd(struct cardstate *cs,
  1739. const unsigned char *buf, int len,
  1740. struct tasklet_struct *wake_tasklet)
  1741. {
  1742. struct cmdbuf_t *cb;
  1743. unsigned long flags;
  1744. int rc;
  1745. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  1746. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  1747. "CMD Transmit", len, buf);
  1748. if (len <= 0) {
  1749. /* nothing to do */
  1750. rc = 0;
  1751. goto notqueued;
  1752. }
  1753. /* translate "+++" escape sequence sent as a single separate command
  1754. * into "close AT channel" command for error recovery
  1755. * The next command will reopen the AT channel automatically.
  1756. */
  1757. if (len == 3 && !memcmp(buf, "+++", 3)) {
  1758. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
  1759. goto notqueued;
  1760. }
  1761. if (len > IF_WRITEBUF)
  1762. len = IF_WRITEBUF;
  1763. if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
  1764. dev_err(cs->dev, "%s: out of memory\n", __func__);
  1765. rc = -ENOMEM;
  1766. goto notqueued;
  1767. }
  1768. memcpy(cb->buf, buf, len);
  1769. cb->len = len;
  1770. cb->offset = 0;
  1771. cb->next = NULL;
  1772. cb->wake_tasklet = wake_tasklet;
  1773. spin_lock_irqsave(&cs->cmdlock, flags);
  1774. cb->prev = cs->lastcmdbuf;
  1775. if (cs->lastcmdbuf)
  1776. cs->lastcmdbuf->next = cb;
  1777. else {
  1778. cs->cmdbuf = cb;
  1779. cs->curlen = len;
  1780. }
  1781. cs->cmdbytes += len;
  1782. cs->lastcmdbuf = cb;
  1783. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1784. spin_lock_irqsave(&cs->lock, flags);
  1785. if (unlikely(!cs->connected)) {
  1786. spin_unlock_irqrestore(&cs->lock, flags);
  1787. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1788. /* flush command queue */
  1789. spin_lock_irqsave(&cs->cmdlock, flags);
  1790. while (cs->cmdbuf != NULL)
  1791. complete_cb(cs);
  1792. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1793. return -ENODEV;
  1794. }
  1795. rc = start_cbsend(cs);
  1796. spin_unlock_irqrestore(&cs->lock, flags);
  1797. return rc < 0 ? rc : len;
  1798. notqueued: /* request handled without queuing */
  1799. if (wake_tasklet)
  1800. tasklet_schedule(wake_tasklet);
  1801. return rc;
  1802. }
  1803. /* gigaset_write_room
  1804. * tty_driver.write_room interface routine
  1805. * return number of characters the driver will accept to be written via
  1806. * gigaset_write_cmd
  1807. * parameter:
  1808. * controller state structure
  1809. * return value:
  1810. * number of characters
  1811. */
  1812. static int gigaset_write_room(struct cardstate *cs)
  1813. {
  1814. return IF_WRITEBUF;
  1815. }
  1816. /* gigaset_chars_in_buffer
  1817. * tty_driver.chars_in_buffer interface routine
  1818. * return number of characters waiting to be sent
  1819. * parameter:
  1820. * controller state structure
  1821. * return value:
  1822. * number of characters
  1823. */
  1824. static int gigaset_chars_in_buffer(struct cardstate *cs)
  1825. {
  1826. return cs->cmdbytes;
  1827. }
  1828. /* gigaset_brkchars
  1829. * implementation of ioctl(GIGASET_BRKCHARS)
  1830. * parameter:
  1831. * controller state structure
  1832. * return value:
  1833. * -EINVAL (unimplemented function)
  1834. */
  1835. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  1836. {
  1837. return -EINVAL;
  1838. }
  1839. /* Device Initialization/Shutdown */
  1840. /* ============================== */
  1841. /* Free hardware dependent part of the B channel structure
  1842. * parameter:
  1843. * bcs B channel structure
  1844. * return value:
  1845. * !=0 on success
  1846. */
  1847. static int gigaset_freebcshw(struct bc_state *bcs)
  1848. {
  1849. struct bas_bc_state *ubc = bcs->hw.bas;
  1850. int i;
  1851. if (!ubc)
  1852. return 0;
  1853. /* kill URBs and tasklets before freeing - better safe than sorry */
  1854. ubc->running = 0;
  1855. gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
  1856. for (i = 0; i < BAS_OUTURBS; ++i) {
  1857. usb_kill_urb(ubc->isoouturbs[i].urb);
  1858. usb_free_urb(ubc->isoouturbs[i].urb);
  1859. }
  1860. for (i = 0; i < BAS_INURBS; ++i) {
  1861. usb_kill_urb(ubc->isoinurbs[i]);
  1862. usb_free_urb(ubc->isoinurbs[i]);
  1863. }
  1864. tasklet_kill(&ubc->sent_tasklet);
  1865. tasklet_kill(&ubc->rcvd_tasklet);
  1866. kfree(ubc->isooutbuf);
  1867. kfree(ubc);
  1868. bcs->hw.bas = NULL;
  1869. return 1;
  1870. }
  1871. /* Initialize hardware dependent part of the B channel structure
  1872. * parameter:
  1873. * bcs B channel structure
  1874. * return value:
  1875. * !=0 on success
  1876. */
  1877. static int gigaset_initbcshw(struct bc_state *bcs)
  1878. {
  1879. int i;
  1880. struct bas_bc_state *ubc;
  1881. bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
  1882. if (!ubc) {
  1883. pr_err("out of memory\n");
  1884. return 0;
  1885. }
  1886. ubc->running = 0;
  1887. atomic_set(&ubc->corrbytes, 0);
  1888. spin_lock_init(&ubc->isooutlock);
  1889. for (i = 0; i < BAS_OUTURBS; ++i) {
  1890. ubc->isoouturbs[i].urb = NULL;
  1891. ubc->isoouturbs[i].bcs = bcs;
  1892. }
  1893. ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
  1894. ubc->numsub = 0;
  1895. if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
  1896. pr_err("out of memory\n");
  1897. kfree(ubc);
  1898. bcs->hw.bas = NULL;
  1899. return 0;
  1900. }
  1901. tasklet_init(&ubc->sent_tasklet,
  1902. &write_iso_tasklet, (unsigned long) bcs);
  1903. spin_lock_init(&ubc->isoinlock);
  1904. for (i = 0; i < BAS_INURBS; ++i)
  1905. ubc->isoinurbs[i] = NULL;
  1906. ubc->isoindone = NULL;
  1907. ubc->loststatus = -EINPROGRESS;
  1908. ubc->isoinlost = 0;
  1909. ubc->seqlen = 0;
  1910. ubc->inbyte = 0;
  1911. ubc->inbits = 0;
  1912. ubc->goodbytes = 0;
  1913. ubc->alignerrs = 0;
  1914. ubc->fcserrs = 0;
  1915. ubc->frameerrs = 0;
  1916. ubc->giants = 0;
  1917. ubc->runts = 0;
  1918. ubc->aborts = 0;
  1919. ubc->shared0s = 0;
  1920. ubc->stolen0s = 0;
  1921. tasklet_init(&ubc->rcvd_tasklet,
  1922. &read_iso_tasklet, (unsigned long) bcs);
  1923. return 1;
  1924. }
  1925. static void gigaset_reinitbcshw(struct bc_state *bcs)
  1926. {
  1927. struct bas_bc_state *ubc = bcs->hw.bas;
  1928. bcs->hw.bas->running = 0;
  1929. atomic_set(&bcs->hw.bas->corrbytes, 0);
  1930. bcs->hw.bas->numsub = 0;
  1931. spin_lock_init(&ubc->isooutlock);
  1932. spin_lock_init(&ubc->isoinlock);
  1933. ubc->loststatus = -EINPROGRESS;
  1934. }
  1935. static void gigaset_freecshw(struct cardstate *cs)
  1936. {
  1937. /* timers, URBs and rcvbuf are disposed of in disconnect */
  1938. kfree(cs->hw.bas->int_in_buf);
  1939. kfree(cs->hw.bas);
  1940. cs->hw.bas = NULL;
  1941. }
  1942. static int gigaset_initcshw(struct cardstate *cs)
  1943. {
  1944. struct bas_cardstate *ucs;
  1945. cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
  1946. if (!ucs) {
  1947. pr_err("out of memory\n");
  1948. return 0;
  1949. }
  1950. ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
  1951. if (!ucs->int_in_buf) {
  1952. kfree(ucs);
  1953. pr_err("out of memory\n");
  1954. return 0;
  1955. }
  1956. ucs->urb_cmd_in = NULL;
  1957. ucs->urb_cmd_out = NULL;
  1958. ucs->rcvbuf = NULL;
  1959. ucs->rcvbuf_size = 0;
  1960. spin_lock_init(&ucs->lock);
  1961. ucs->pending = 0;
  1962. ucs->basstate = 0;
  1963. init_timer(&ucs->timer_ctrl);
  1964. init_timer(&ucs->timer_atrdy);
  1965. init_timer(&ucs->timer_cmd_in);
  1966. init_waitqueue_head(&ucs->waitqueue);
  1967. return 1;
  1968. }
  1969. /* freeurbs
  1970. * unlink and deallocate all URBs unconditionally
  1971. * caller must make sure that no commands are still in progress
  1972. * parameter:
  1973. * cs controller state structure
  1974. */
  1975. static void freeurbs(struct cardstate *cs)
  1976. {
  1977. struct bas_cardstate *ucs = cs->hw.bas;
  1978. struct bas_bc_state *ubc;
  1979. int i, j;
  1980. gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
  1981. for (j = 0; j < BAS_CHANNELS; ++j) {
  1982. ubc = cs->bcs[j].hw.bas;
  1983. for (i = 0; i < BAS_OUTURBS; ++i) {
  1984. usb_kill_urb(ubc->isoouturbs[i].urb);
  1985. usb_free_urb(ubc->isoouturbs[i].urb);
  1986. ubc->isoouturbs[i].urb = NULL;
  1987. }
  1988. for (i = 0; i < BAS_INURBS; ++i) {
  1989. usb_kill_urb(ubc->isoinurbs[i]);
  1990. usb_free_urb(ubc->isoinurbs[i]);
  1991. ubc->isoinurbs[i] = NULL;
  1992. }
  1993. }
  1994. usb_kill_urb(ucs->urb_int_in);
  1995. usb_free_urb(ucs->urb_int_in);
  1996. ucs->urb_int_in = NULL;
  1997. usb_kill_urb(ucs->urb_cmd_out);
  1998. usb_free_urb(ucs->urb_cmd_out);
  1999. ucs->urb_cmd_out = NULL;
  2000. usb_kill_urb(ucs->urb_cmd_in);
  2001. usb_free_urb(ucs->urb_cmd_in);
  2002. ucs->urb_cmd_in = NULL;
  2003. usb_kill_urb(ucs->urb_ctrl);
  2004. usb_free_urb(ucs->urb_ctrl);
  2005. ucs->urb_ctrl = NULL;
  2006. }
  2007. /* gigaset_probe
  2008. * This function is called when a new USB device is connected.
  2009. * It checks whether the new device is handled by this driver.
  2010. */
  2011. static int gigaset_probe(struct usb_interface *interface,
  2012. const struct usb_device_id *id)
  2013. {
  2014. struct usb_host_interface *hostif;
  2015. struct usb_device *udev = interface_to_usbdev(interface);
  2016. struct cardstate *cs = NULL;
  2017. struct bas_cardstate *ucs = NULL;
  2018. struct bas_bc_state *ubc;
  2019. struct usb_endpoint_descriptor *endpoint;
  2020. int i, j;
  2021. int rc;
  2022. gig_dbg(DEBUG_ANY,
  2023. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  2024. __func__, le16_to_cpu(udev->descriptor.idVendor),
  2025. le16_to_cpu(udev->descriptor.idProduct));
  2026. /* set required alternate setting */
  2027. hostif = interface->cur_altsetting;
  2028. if (hostif->desc.bAlternateSetting != 3) {
  2029. gig_dbg(DEBUG_ANY,
  2030. "%s: wrong alternate setting %d - trying to switch",
  2031. __func__, hostif->desc.bAlternateSetting);
  2032. if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
  2033. dev_warn(&udev->dev, "usb_set_interface failed, "
  2034. "device %d interface %d altsetting %d\n",
  2035. udev->devnum, hostif->desc.bInterfaceNumber,
  2036. hostif->desc.bAlternateSetting);
  2037. return -ENODEV;
  2038. }
  2039. hostif = interface->cur_altsetting;
  2040. }
  2041. /* Reject application specific interfaces
  2042. */
  2043. if (hostif->desc.bInterfaceClass != 255) {
  2044. dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
  2045. __func__, hostif->desc.bInterfaceClass);
  2046. return -ENODEV;
  2047. }
  2048. dev_info(&udev->dev,
  2049. "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
  2050. __func__, le16_to_cpu(udev->descriptor.idVendor),
  2051. le16_to_cpu(udev->descriptor.idProduct));
  2052. /* allocate memory for our device state and intialize it */
  2053. cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
  2054. GIGASET_MODULENAME);
  2055. if (!cs)
  2056. return -ENODEV;
  2057. ucs = cs->hw.bas;
  2058. /* save off device structure ptrs for later use */
  2059. usb_get_dev(udev);
  2060. ucs->udev = udev;
  2061. ucs->interface = interface;
  2062. cs->dev = &interface->dev;
  2063. /* allocate URBs:
  2064. * - one for the interrupt pipe
  2065. * - three for the different uses of the default control pipe
  2066. * - three for each isochronous pipe
  2067. */
  2068. if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2069. !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2070. !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
  2071. !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  2072. goto allocerr;
  2073. for (j = 0; j < BAS_CHANNELS; ++j) {
  2074. ubc = cs->bcs[j].hw.bas;
  2075. for (i = 0; i < BAS_OUTURBS; ++i)
  2076. if (!(ubc->isoouturbs[i].urb =
  2077. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2078. goto allocerr;
  2079. for (i = 0; i < BAS_INURBS; ++i)
  2080. if (!(ubc->isoinurbs[i] =
  2081. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2082. goto allocerr;
  2083. }
  2084. ucs->rcvbuf = NULL;
  2085. ucs->rcvbuf_size = 0;
  2086. /* Fill the interrupt urb and send it to the core */
  2087. endpoint = &hostif->endpoint[0].desc;
  2088. usb_fill_int_urb(ucs->urb_int_in, udev,
  2089. usb_rcvintpipe(udev,
  2090. (endpoint->bEndpointAddress) & 0x0f),
  2091. ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
  2092. endpoint->bInterval);
  2093. if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
  2094. dev_err(cs->dev, "could not submit interrupt URB: %s\n",
  2095. get_usb_rcmsg(rc));
  2096. goto error;
  2097. }
  2098. /* tell the device that the driver is ready */
  2099. if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
  2100. goto error;
  2101. /* tell common part that the device is ready */
  2102. if (startmode == SM_LOCKED)
  2103. cs->mstate = MS_LOCKED;
  2104. /* save address of controller structure */
  2105. usb_set_intfdata(interface, cs);
  2106. if (!gigaset_start(cs))
  2107. goto error;
  2108. return 0;
  2109. allocerr:
  2110. dev_err(cs->dev, "could not allocate URBs\n");
  2111. error:
  2112. freeurbs(cs);
  2113. usb_set_intfdata(interface, NULL);
  2114. gigaset_freecs(cs);
  2115. return -ENODEV;
  2116. }
  2117. /* gigaset_disconnect
  2118. * This function is called when the Gigaset base is unplugged.
  2119. */
  2120. static void gigaset_disconnect(struct usb_interface *interface)
  2121. {
  2122. struct cardstate *cs;
  2123. struct bas_cardstate *ucs;
  2124. int j;
  2125. cs = usb_get_intfdata(interface);
  2126. ucs = cs->hw.bas;
  2127. dev_info(cs->dev, "disconnecting Gigaset base\n");
  2128. /* mark base as not ready, all channels disconnected */
  2129. ucs->basstate = 0;
  2130. /* tell LL all channels are down */
  2131. for (j = 0; j < BAS_CHANNELS; ++j)
  2132. gigaset_bchannel_down(cs->bcs + j);
  2133. /* stop driver (common part) */
  2134. gigaset_stop(cs);
  2135. /* stop timers and URBs, free ressources */
  2136. del_timer_sync(&ucs->timer_ctrl);
  2137. del_timer_sync(&ucs->timer_atrdy);
  2138. del_timer_sync(&ucs->timer_cmd_in);
  2139. freeurbs(cs);
  2140. usb_set_intfdata(interface, NULL);
  2141. kfree(ucs->rcvbuf);
  2142. ucs->rcvbuf = NULL;
  2143. ucs->rcvbuf_size = 0;
  2144. usb_put_dev(ucs->udev);
  2145. ucs->interface = NULL;
  2146. ucs->udev = NULL;
  2147. cs->dev = NULL;
  2148. gigaset_freecs(cs);
  2149. }
  2150. /* gigaset_suspend
  2151. * This function is called before the USB connection is suspended.
  2152. */
  2153. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
  2154. {
  2155. struct cardstate *cs = usb_get_intfdata(intf);
  2156. struct bas_cardstate *ucs = cs->hw.bas;
  2157. int rc;
  2158. /* set suspend flag; this stops AT command/response traffic */
  2159. if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
  2160. gig_dbg(DEBUG_SUSPEND, "already suspended");
  2161. return 0;
  2162. }
  2163. /* wait a bit for blocking conditions to go away */
  2164. rc = wait_event_timeout(ucs->waitqueue,
  2165. !(ucs->basstate &
  2166. (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
  2167. BAS_TIMEOUT*HZ/10);
  2168. gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
  2169. /* check for conditions preventing suspend */
  2170. if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
  2171. dev_warn(cs->dev, "cannot suspend:\n");
  2172. if (ucs->basstate & BS_B1OPEN)
  2173. dev_warn(cs->dev, " B channel 1 open\n");
  2174. if (ucs->basstate & BS_B2OPEN)
  2175. dev_warn(cs->dev, " B channel 2 open\n");
  2176. if (ucs->basstate & BS_ATRDPEND)
  2177. dev_warn(cs->dev, " receiving AT reply\n");
  2178. if (ucs->basstate & BS_ATWRPEND)
  2179. dev_warn(cs->dev, " sending AT command\n");
  2180. update_basstate(ucs, 0, BS_SUSPEND);
  2181. return -EBUSY;
  2182. }
  2183. /* close AT channel if open */
  2184. if (ucs->basstate & BS_ATOPEN) {
  2185. gig_dbg(DEBUG_SUSPEND, "closing AT channel");
  2186. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
  2187. if (rc) {
  2188. update_basstate(ucs, 0, BS_SUSPEND);
  2189. return rc;
  2190. }
  2191. wait_event_timeout(ucs->waitqueue, !ucs->pending,
  2192. BAS_TIMEOUT*HZ/10);
  2193. /* in case of timeout, proceed anyway */
  2194. }
  2195. /* kill all URBs and timers that might still be pending */
  2196. usb_kill_urb(ucs->urb_ctrl);
  2197. usb_kill_urb(ucs->urb_int_in);
  2198. del_timer_sync(&ucs->timer_ctrl);
  2199. gig_dbg(DEBUG_SUSPEND, "suspend complete");
  2200. return 0;
  2201. }
  2202. /* gigaset_resume
  2203. * This function is called after the USB connection has been resumed.
  2204. */
  2205. static int gigaset_resume(struct usb_interface *intf)
  2206. {
  2207. struct cardstate *cs = usb_get_intfdata(intf);
  2208. struct bas_cardstate *ucs = cs->hw.bas;
  2209. int rc;
  2210. /* resubmit interrupt URB for spontaneous messages from base */
  2211. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2212. if (rc) {
  2213. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  2214. get_usb_rcmsg(rc));
  2215. return rc;
  2216. }
  2217. /* clear suspend flag to reallow activity */
  2218. update_basstate(ucs, 0, BS_SUSPEND);
  2219. gig_dbg(DEBUG_SUSPEND, "resume complete");
  2220. return 0;
  2221. }
  2222. /* gigaset_pre_reset
  2223. * This function is called before the USB connection is reset.
  2224. */
  2225. static int gigaset_pre_reset(struct usb_interface *intf)
  2226. {
  2227. /* handle just like suspend */
  2228. return gigaset_suspend(intf, PMSG_ON);
  2229. }
  2230. /* gigaset_post_reset
  2231. * This function is called after the USB connection has been reset.
  2232. */
  2233. static int gigaset_post_reset(struct usb_interface *intf)
  2234. {
  2235. /* FIXME: send HD_DEVICE_INIT_ACK? */
  2236. /* resume operations */
  2237. return gigaset_resume(intf);
  2238. }
  2239. static const struct gigaset_ops gigops = {
  2240. gigaset_write_cmd,
  2241. gigaset_write_room,
  2242. gigaset_chars_in_buffer,
  2243. gigaset_brkchars,
  2244. gigaset_init_bchannel,
  2245. gigaset_close_bchannel,
  2246. gigaset_initbcshw,
  2247. gigaset_freebcshw,
  2248. gigaset_reinitbcshw,
  2249. gigaset_initcshw,
  2250. gigaset_freecshw,
  2251. gigaset_set_modem_ctrl,
  2252. gigaset_baud_rate,
  2253. gigaset_set_line_ctrl,
  2254. gigaset_isoc_send_skb,
  2255. gigaset_isoc_input,
  2256. };
  2257. /* bas_gigaset_init
  2258. * This function is called after the kernel module is loaded.
  2259. */
  2260. static int __init bas_gigaset_init(void)
  2261. {
  2262. int result;
  2263. /* allocate memory for our driver state and intialize it */
  2264. if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  2265. GIGASET_MODULENAME, GIGASET_DEVNAME,
  2266. &gigops, THIS_MODULE)) == NULL)
  2267. goto error;
  2268. /* register this driver with the USB subsystem */
  2269. result = usb_register(&gigaset_usb_driver);
  2270. if (result < 0) {
  2271. pr_err("error %d registering USB driver\n", -result);
  2272. goto error;
  2273. }
  2274. pr_info(DRIVER_DESC "\n");
  2275. return 0;
  2276. error:
  2277. if (driver)
  2278. gigaset_freedriver(driver);
  2279. driver = NULL;
  2280. return -1;
  2281. }
  2282. /* bas_gigaset_exit
  2283. * This function is called before the kernel module is unloaded.
  2284. */
  2285. static void __exit bas_gigaset_exit(void)
  2286. {
  2287. struct bas_cardstate *ucs;
  2288. int i;
  2289. gigaset_blockdriver(driver); /* => probe will fail
  2290. * => no gigaset_start any more
  2291. */
  2292. /* stop all connected devices */
  2293. for (i = 0; i < driver->minors; i++) {
  2294. if (gigaset_shutdown(driver->cs + i) < 0)
  2295. continue; /* no device */
  2296. /* from now on, no isdn callback should be possible */
  2297. /* close all still open channels */
  2298. ucs = driver->cs[i].hw.bas;
  2299. if (ucs->basstate & BS_B1OPEN) {
  2300. gig_dbg(DEBUG_INIT, "closing B1 channel");
  2301. usb_control_msg(ucs->udev,
  2302. usb_sndctrlpipe(ucs->udev, 0),
  2303. HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
  2304. 0, 0, NULL, 0, BAS_TIMEOUT);
  2305. }
  2306. if (ucs->basstate & BS_B2OPEN) {
  2307. gig_dbg(DEBUG_INIT, "closing B2 channel");
  2308. usb_control_msg(ucs->udev,
  2309. usb_sndctrlpipe(ucs->udev, 0),
  2310. HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
  2311. 0, 0, NULL, 0, BAS_TIMEOUT);
  2312. }
  2313. if (ucs->basstate & BS_ATOPEN) {
  2314. gig_dbg(DEBUG_INIT, "closing AT channel");
  2315. usb_control_msg(ucs->udev,
  2316. usb_sndctrlpipe(ucs->udev, 0),
  2317. HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
  2318. 0, 0, NULL, 0, BAS_TIMEOUT);
  2319. }
  2320. ucs->basstate = 0;
  2321. }
  2322. /* deregister this driver with the USB subsystem */
  2323. usb_deregister(&gigaset_usb_driver);
  2324. /* this will call the disconnect-callback */
  2325. /* from now on, no disconnect/probe callback should be running */
  2326. gigaset_freedriver(driver);
  2327. driver = NULL;
  2328. }
  2329. module_init(bas_gigaset_init);
  2330. module_exit(bas_gigaset_exit);
  2331. MODULE_AUTHOR(DRIVER_AUTHOR);
  2332. MODULE_DESCRIPTION(DRIVER_DESC);
  2333. MODULE_LICENSE("GPL");