hso.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. /*****************************************************************************/
  96. /* Debugging functions */
  97. /*****************************************************************************/
  98. #define D__(lvl_, fmt, arg...) \
  99. do { \
  100. printk(lvl_ "[%d:%s]: " fmt "\n", \
  101. __LINE__, __func__, ## arg); \
  102. } while (0)
  103. #define D_(lvl, args...) \
  104. do { \
  105. if (lvl & debug) \
  106. D__(KERN_INFO, args); \
  107. } while (0)
  108. #define D1(args...) D_(0x01, ##args)
  109. #define D2(args...) D_(0x02, ##args)
  110. #define D3(args...) D_(0x04, ##args)
  111. #define D4(args...) D_(0x08, ##args)
  112. #define D5(args...) D_(0x10, ##args)
  113. /*****************************************************************************/
  114. /* Enumerators */
  115. /*****************************************************************************/
  116. enum pkt_parse_state {
  117. WAIT_IP,
  118. WAIT_DATA,
  119. WAIT_SYNC
  120. };
  121. /*****************************************************************************/
  122. /* Structs */
  123. /*****************************************************************************/
  124. struct hso_shared_int {
  125. struct usb_endpoint_descriptor *intr_endp;
  126. void *shared_intr_buf;
  127. struct urb *shared_intr_urb;
  128. struct usb_device *usb;
  129. int use_count;
  130. int ref_count;
  131. struct mutex shared_int_lock;
  132. };
  133. struct hso_net {
  134. struct hso_device *parent;
  135. struct net_device *net;
  136. struct rfkill *rfkill;
  137. struct usb_endpoint_descriptor *in_endp;
  138. struct usb_endpoint_descriptor *out_endp;
  139. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  140. struct urb *mux_bulk_tx_urb;
  141. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  142. void *mux_bulk_tx_buf;
  143. struct sk_buff *skb_rx_buf;
  144. struct sk_buff *skb_tx_buf;
  145. enum pkt_parse_state rx_parse_state;
  146. spinlock_t net_lock;
  147. unsigned short rx_buf_size;
  148. unsigned short rx_buf_missing;
  149. struct iphdr rx_ip_hdr;
  150. unsigned long flags;
  151. };
  152. enum rx_ctrl_state{
  153. RX_IDLE,
  154. RX_SENT,
  155. RX_PENDING
  156. };
  157. #define BM_REQUEST_TYPE (0xa1)
  158. #define B_NOTIFICATION (0x20)
  159. #define W_VALUE (0x0)
  160. #define W_INDEX (0x2)
  161. #define W_LENGTH (0x2)
  162. #define B_OVERRUN (0x1<<6)
  163. #define B_PARITY (0x1<<5)
  164. #define B_FRAMING (0x1<<4)
  165. #define B_RING_SIGNAL (0x1<<3)
  166. #define B_BREAK (0x1<<2)
  167. #define B_TX_CARRIER (0x1<<1)
  168. #define B_RX_CARRIER (0x1<<0)
  169. struct hso_serial_state_notification {
  170. u8 bmRequestType;
  171. u8 bNotification;
  172. u16 wValue;
  173. u16 wIndex;
  174. u16 wLength;
  175. u16 UART_state_bitmap;
  176. } __packed;
  177. struct hso_tiocmget {
  178. struct mutex mutex;
  179. wait_queue_head_t waitq;
  180. int intr_completed;
  181. struct usb_endpoint_descriptor *endp;
  182. struct urb *urb;
  183. struct hso_serial_state_notification serial_state_notification;
  184. u16 prev_UART_state_bitmap;
  185. struct uart_icount icount;
  186. };
  187. struct hso_serial {
  188. struct hso_device *parent;
  189. int magic;
  190. u8 minor;
  191. struct hso_shared_int *shared_int;
  192. /* rx/tx urb could be either a bulk urb or a control urb depending
  193. on which serial port it is used on. */
  194. struct urb *rx_urb[MAX_RX_URBS];
  195. u8 num_rx_urbs;
  196. u8 *rx_data[MAX_RX_URBS];
  197. u16 rx_data_length; /* should contain allocated length */
  198. struct urb *tx_urb;
  199. u8 *tx_data;
  200. u8 *tx_buffer;
  201. u16 tx_data_length; /* should contain allocated length */
  202. u16 tx_data_count;
  203. u16 tx_buffer_count;
  204. struct usb_ctrlrequest ctrl_req_tx;
  205. struct usb_ctrlrequest ctrl_req_rx;
  206. struct usb_endpoint_descriptor *in_endp;
  207. struct usb_endpoint_descriptor *out_endp;
  208. enum rx_ctrl_state rx_state;
  209. u8 rts_state;
  210. u8 dtr_state;
  211. unsigned tx_urb_used:1;
  212. struct tty_port port;
  213. /* from usb_serial_port */
  214. spinlock_t serial_lock;
  215. int (*write_data) (struct hso_serial *serial);
  216. struct hso_tiocmget *tiocmget;
  217. /* Hacks required to get flow control
  218. * working on the serial receive buffers
  219. * so as not to drop characters on the floor.
  220. */
  221. int curr_rx_urb_idx;
  222. u16 curr_rx_urb_offset;
  223. u8 rx_urb_filled[MAX_RX_URBS];
  224. struct tasklet_struct unthrottle_tasklet;
  225. struct work_struct retry_unthrottle_workqueue;
  226. };
  227. struct hso_device {
  228. union {
  229. struct hso_serial *dev_serial;
  230. struct hso_net *dev_net;
  231. } port_data;
  232. u32 port_spec;
  233. u8 is_active;
  234. u8 usb_gone;
  235. struct work_struct async_get_intf;
  236. struct work_struct async_put_intf;
  237. struct work_struct reset_device;
  238. struct usb_device *usb;
  239. struct usb_interface *interface;
  240. struct device *dev;
  241. struct kref ref;
  242. struct mutex mutex;
  243. };
  244. /* Type of interface */
  245. #define HSO_INTF_MASK 0xFF00
  246. #define HSO_INTF_MUX 0x0100
  247. #define HSO_INTF_BULK 0x0200
  248. /* Type of port */
  249. #define HSO_PORT_MASK 0xFF
  250. #define HSO_PORT_NO_PORT 0x0
  251. #define HSO_PORT_CONTROL 0x1
  252. #define HSO_PORT_APP 0x2
  253. #define HSO_PORT_GPS 0x3
  254. #define HSO_PORT_PCSC 0x4
  255. #define HSO_PORT_APP2 0x5
  256. #define HSO_PORT_GPS_CONTROL 0x6
  257. #define HSO_PORT_MSD 0x7
  258. #define HSO_PORT_VOICE 0x8
  259. #define HSO_PORT_DIAG2 0x9
  260. #define HSO_PORT_DIAG 0x10
  261. #define HSO_PORT_MODEM 0x11
  262. #define HSO_PORT_NETWORK 0x12
  263. /* Additional device info */
  264. #define HSO_INFO_MASK 0xFF000000
  265. #define HSO_INFO_CRC_BUG 0x01000000
  266. /*****************************************************************************/
  267. /* Prototypes */
  268. /*****************************************************************************/
  269. /* Serial driver functions */
  270. static int hso_serial_tiocmset(struct tty_struct *tty,
  271. unsigned int set, unsigned int clear);
  272. static void ctrl_callback(struct urb *urb);
  273. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  274. static void hso_kick_transmit(struct hso_serial *serial);
  275. /* Helper functions */
  276. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  277. struct usb_device *usb, gfp_t gfp);
  278. static void handle_usb_error(int status, const char *function,
  279. struct hso_device *hso_dev);
  280. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  281. int type, int dir);
  282. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  283. static void hso_free_interface(struct usb_interface *intf);
  284. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  285. static int hso_stop_serial_device(struct hso_device *hso_dev);
  286. static int hso_start_net_device(struct hso_device *hso_dev);
  287. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  288. static int hso_stop_net_device(struct hso_device *hso_dev);
  289. static void hso_serial_ref_free(struct kref *ref);
  290. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  291. static int hso_mux_serial_read(struct hso_serial *serial);
  292. static void async_get_intf(struct work_struct *data);
  293. static void async_put_intf(struct work_struct *data);
  294. static int hso_put_activity(struct hso_device *hso_dev);
  295. static int hso_get_activity(struct hso_device *hso_dev);
  296. static void tiocmget_intr_callback(struct urb *urb);
  297. static void reset_device(struct work_struct *data);
  298. /*****************************************************************************/
  299. /* Helping functions */
  300. /*****************************************************************************/
  301. /* #define DEBUG */
  302. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  303. {
  304. return hso_dev->port_data.dev_net;
  305. }
  306. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  307. {
  308. return hso_dev->port_data.dev_serial;
  309. }
  310. /* Debugging functions */
  311. #ifdef DEBUG
  312. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  313. unsigned int len)
  314. {
  315. static char name[255];
  316. sprintf(name, "hso[%d:%s]", line_count, func_name);
  317. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  318. }
  319. #define DUMP(buf_, len_) \
  320. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  321. #define DUMP1(buf_, len_) \
  322. do { \
  323. if (0x01 & debug) \
  324. DUMP(buf_, len_); \
  325. } while (0)
  326. #else
  327. #define DUMP(buf_, len_)
  328. #define DUMP1(buf_, len_)
  329. #endif
  330. /* module parameters */
  331. static int debug;
  332. static int tty_major;
  333. static int disable_net;
  334. /* driver info */
  335. static const char driver_name[] = "hso";
  336. static const char tty_filename[] = "ttyHS";
  337. static const char *version = __FILE__ ": " MOD_AUTHOR;
  338. /* the usb driver itself (registered in hso_init) */
  339. static struct usb_driver hso_driver;
  340. /* serial structures */
  341. static struct tty_driver *tty_drv;
  342. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  343. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  344. static spinlock_t serial_table_lock;
  345. static const s32 default_port_spec[] = {
  346. HSO_INTF_MUX | HSO_PORT_NETWORK,
  347. HSO_INTF_BULK | HSO_PORT_DIAG,
  348. HSO_INTF_BULK | HSO_PORT_MODEM,
  349. 0
  350. };
  351. static const s32 icon321_port_spec[] = {
  352. HSO_INTF_MUX | HSO_PORT_NETWORK,
  353. HSO_INTF_BULK | HSO_PORT_DIAG2,
  354. HSO_INTF_BULK | HSO_PORT_MODEM,
  355. HSO_INTF_BULK | HSO_PORT_DIAG,
  356. 0
  357. };
  358. #define default_port_device(vendor, product) \
  359. USB_DEVICE(vendor, product), \
  360. .driver_info = (kernel_ulong_t)default_port_spec
  361. #define icon321_port_device(vendor, product) \
  362. USB_DEVICE(vendor, product), \
  363. .driver_info = (kernel_ulong_t)icon321_port_spec
  364. /* list of devices we support */
  365. static const struct usb_device_id hso_ids[] = {
  366. {default_port_device(0x0af0, 0x6711)},
  367. {default_port_device(0x0af0, 0x6731)},
  368. {default_port_device(0x0af0, 0x6751)},
  369. {default_port_device(0x0af0, 0x6771)},
  370. {default_port_device(0x0af0, 0x6791)},
  371. {default_port_device(0x0af0, 0x6811)},
  372. {default_port_device(0x0af0, 0x6911)},
  373. {default_port_device(0x0af0, 0x6951)},
  374. {default_port_device(0x0af0, 0x6971)},
  375. {default_port_device(0x0af0, 0x7011)},
  376. {default_port_device(0x0af0, 0x7031)},
  377. {default_port_device(0x0af0, 0x7051)},
  378. {default_port_device(0x0af0, 0x7071)},
  379. {default_port_device(0x0af0, 0x7111)},
  380. {default_port_device(0x0af0, 0x7211)},
  381. {default_port_device(0x0af0, 0x7251)},
  382. {default_port_device(0x0af0, 0x7271)},
  383. {default_port_device(0x0af0, 0x7311)},
  384. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  385. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  386. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  387. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  388. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  389. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  390. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  391. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  392. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  393. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7701)},
  395. {USB_DEVICE(0x0af0, 0x7706)},
  396. {USB_DEVICE(0x0af0, 0x7801)},
  397. {USB_DEVICE(0x0af0, 0x7901)},
  398. {USB_DEVICE(0x0af0, 0x7A01)},
  399. {USB_DEVICE(0x0af0, 0x7A05)},
  400. {USB_DEVICE(0x0af0, 0x8200)},
  401. {USB_DEVICE(0x0af0, 0x8201)},
  402. {USB_DEVICE(0x0af0, 0x8300)},
  403. {USB_DEVICE(0x0af0, 0x8302)},
  404. {USB_DEVICE(0x0af0, 0x8304)},
  405. {USB_DEVICE(0x0af0, 0x8400)},
  406. {USB_DEVICE(0x0af0, 0x8600)},
  407. {USB_DEVICE(0x0af0, 0x8800)},
  408. {USB_DEVICE(0x0af0, 0x8900)},
  409. {USB_DEVICE(0x0af0, 0x9000)},
  410. {USB_DEVICE(0x0af0, 0xd035)},
  411. {USB_DEVICE(0x0af0, 0xd055)},
  412. {USB_DEVICE(0x0af0, 0xd155)},
  413. {USB_DEVICE(0x0af0, 0xd255)},
  414. {USB_DEVICE(0x0af0, 0xd057)},
  415. {USB_DEVICE(0x0af0, 0xd157)},
  416. {USB_DEVICE(0x0af0, 0xd257)},
  417. {USB_DEVICE(0x0af0, 0xd357)},
  418. {USB_DEVICE(0x0af0, 0xd058)},
  419. {USB_DEVICE(0x0af0, 0xc100)},
  420. {}
  421. };
  422. MODULE_DEVICE_TABLE(usb, hso_ids);
  423. /* Sysfs attribute */
  424. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  425. struct device_attribute *attr,
  426. char *buf)
  427. {
  428. struct hso_device *hso_dev = dev_get_drvdata(dev);
  429. char *port_name;
  430. if (!hso_dev)
  431. return 0;
  432. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  433. case HSO_PORT_CONTROL:
  434. port_name = "Control";
  435. break;
  436. case HSO_PORT_APP:
  437. port_name = "Application";
  438. break;
  439. case HSO_PORT_APP2:
  440. port_name = "Application2";
  441. break;
  442. case HSO_PORT_GPS:
  443. port_name = "GPS";
  444. break;
  445. case HSO_PORT_GPS_CONTROL:
  446. port_name = "GPS Control";
  447. break;
  448. case HSO_PORT_PCSC:
  449. port_name = "PCSC";
  450. break;
  451. case HSO_PORT_DIAG:
  452. port_name = "Diagnostic";
  453. break;
  454. case HSO_PORT_DIAG2:
  455. port_name = "Diagnostic2";
  456. break;
  457. case HSO_PORT_MODEM:
  458. port_name = "Modem";
  459. break;
  460. case HSO_PORT_NETWORK:
  461. port_name = "Network";
  462. break;
  463. default:
  464. port_name = "Unknown";
  465. break;
  466. }
  467. return sprintf(buf, "%s\n", port_name);
  468. }
  469. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  470. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  471. {
  472. int idx;
  473. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  474. if (serial->rx_urb[idx] == urb)
  475. return idx;
  476. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  477. return -1;
  478. }
  479. /* converts mux value to a port spec value */
  480. static u32 hso_mux_to_port(int mux)
  481. {
  482. u32 result;
  483. switch (mux) {
  484. case 0x1:
  485. result = HSO_PORT_CONTROL;
  486. break;
  487. case 0x2:
  488. result = HSO_PORT_APP;
  489. break;
  490. case 0x4:
  491. result = HSO_PORT_PCSC;
  492. break;
  493. case 0x8:
  494. result = HSO_PORT_GPS;
  495. break;
  496. case 0x10:
  497. result = HSO_PORT_APP2;
  498. break;
  499. default:
  500. result = HSO_PORT_NO_PORT;
  501. }
  502. return result;
  503. }
  504. /* converts port spec value to a mux value */
  505. static u32 hso_port_to_mux(int port)
  506. {
  507. u32 result;
  508. switch (port & HSO_PORT_MASK) {
  509. case HSO_PORT_CONTROL:
  510. result = 0x0;
  511. break;
  512. case HSO_PORT_APP:
  513. result = 0x1;
  514. break;
  515. case HSO_PORT_PCSC:
  516. result = 0x2;
  517. break;
  518. case HSO_PORT_GPS:
  519. result = 0x3;
  520. break;
  521. case HSO_PORT_APP2:
  522. result = 0x4;
  523. break;
  524. default:
  525. result = 0x0;
  526. }
  527. return result;
  528. }
  529. static struct hso_serial *get_serial_by_shared_int_and_type(
  530. struct hso_shared_int *shared_int,
  531. int mux)
  532. {
  533. int i, port;
  534. port = hso_mux_to_port(mux);
  535. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  536. if (serial_table[i] &&
  537. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  538. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  539. return dev2ser(serial_table[i]);
  540. }
  541. }
  542. return NULL;
  543. }
  544. static struct hso_serial *get_serial_by_index(unsigned index)
  545. {
  546. struct hso_serial *serial = NULL;
  547. unsigned long flags;
  548. spin_lock_irqsave(&serial_table_lock, flags);
  549. if (serial_table[index])
  550. serial = dev2ser(serial_table[index]);
  551. spin_unlock_irqrestore(&serial_table_lock, flags);
  552. return serial;
  553. }
  554. static int get_free_serial_index(void)
  555. {
  556. int index;
  557. unsigned long flags;
  558. spin_lock_irqsave(&serial_table_lock, flags);
  559. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  560. if (serial_table[index] == NULL) {
  561. spin_unlock_irqrestore(&serial_table_lock, flags);
  562. return index;
  563. }
  564. }
  565. spin_unlock_irqrestore(&serial_table_lock, flags);
  566. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  567. return -1;
  568. }
  569. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  570. {
  571. unsigned long flags;
  572. spin_lock_irqsave(&serial_table_lock, flags);
  573. if (serial)
  574. serial_table[index] = serial->parent;
  575. else
  576. serial_table[index] = NULL;
  577. spin_unlock_irqrestore(&serial_table_lock, flags);
  578. }
  579. static void handle_usb_error(int status, const char *function,
  580. struct hso_device *hso_dev)
  581. {
  582. char *explanation;
  583. switch (status) {
  584. case -ENODEV:
  585. explanation = "no device";
  586. break;
  587. case -ENOENT:
  588. explanation = "endpoint not enabled";
  589. break;
  590. case -EPIPE:
  591. explanation = "endpoint stalled";
  592. break;
  593. case -ENOSPC:
  594. explanation = "not enough bandwidth";
  595. break;
  596. case -ESHUTDOWN:
  597. explanation = "device disabled";
  598. break;
  599. case -EHOSTUNREACH:
  600. explanation = "device suspended";
  601. break;
  602. case -EINVAL:
  603. case -EAGAIN:
  604. case -EFBIG:
  605. case -EMSGSIZE:
  606. explanation = "internal error";
  607. break;
  608. case -EILSEQ:
  609. case -EPROTO:
  610. case -ETIME:
  611. case -ETIMEDOUT:
  612. explanation = "protocol error";
  613. if (hso_dev)
  614. schedule_work(&hso_dev->reset_device);
  615. break;
  616. default:
  617. explanation = "unknown status";
  618. break;
  619. }
  620. /* log a meaningful explanation of an USB status */
  621. D1("%s: received USB status - %s (%d)", function, explanation, status);
  622. }
  623. /* Network interface functions */
  624. /* called when net interface is brought up by ifconfig */
  625. static int hso_net_open(struct net_device *net)
  626. {
  627. struct hso_net *odev = netdev_priv(net);
  628. unsigned long flags = 0;
  629. if (!odev) {
  630. dev_err(&net->dev, "No net device !\n");
  631. return -ENODEV;
  632. }
  633. odev->skb_tx_buf = NULL;
  634. /* setup environment */
  635. spin_lock_irqsave(&odev->net_lock, flags);
  636. odev->rx_parse_state = WAIT_IP;
  637. odev->rx_buf_size = 0;
  638. odev->rx_buf_missing = sizeof(struct iphdr);
  639. spin_unlock_irqrestore(&odev->net_lock, flags);
  640. /* We are up and running. */
  641. set_bit(HSO_NET_RUNNING, &odev->flags);
  642. hso_start_net_device(odev->parent);
  643. /* Tell the kernel we are ready to start receiving from it */
  644. netif_start_queue(net);
  645. return 0;
  646. }
  647. /* called when interface is brought down by ifconfig */
  648. static int hso_net_close(struct net_device *net)
  649. {
  650. struct hso_net *odev = netdev_priv(net);
  651. /* we don't need the queue anymore */
  652. netif_stop_queue(net);
  653. /* no longer running */
  654. clear_bit(HSO_NET_RUNNING, &odev->flags);
  655. hso_stop_net_device(odev->parent);
  656. /* done */
  657. return 0;
  658. }
  659. /* USB tells is xmit done, we should start the netqueue again */
  660. static void write_bulk_callback(struct urb *urb)
  661. {
  662. struct hso_net *odev = urb->context;
  663. int status = urb->status;
  664. /* Sanity check */
  665. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  666. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  667. return;
  668. }
  669. /* Do we still have a valid kernel network device? */
  670. if (!netif_device_present(odev->net)) {
  671. dev_err(&urb->dev->dev, "%s: net device not present\n",
  672. __func__);
  673. return;
  674. }
  675. /* log status, but don't act on it, we don't need to resubmit anything
  676. * anyhow */
  677. if (status)
  678. handle_usb_error(status, __func__, odev->parent);
  679. hso_put_activity(odev->parent);
  680. /* Tell the network interface we are ready for another frame */
  681. netif_wake_queue(odev->net);
  682. }
  683. /* called by kernel when we need to transmit a packet */
  684. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  685. struct net_device *net)
  686. {
  687. struct hso_net *odev = netdev_priv(net);
  688. int result;
  689. /* Tell the kernel, "No more frames 'til we are done with this one." */
  690. netif_stop_queue(net);
  691. if (hso_get_activity(odev->parent) == -EAGAIN) {
  692. odev->skb_tx_buf = skb;
  693. return NETDEV_TX_OK;
  694. }
  695. /* log if asked */
  696. DUMP1(skb->data, skb->len);
  697. /* Copy it from kernel memory to OUR memory */
  698. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  699. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  700. /* Fill in the URB for shipping it out. */
  701. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  702. odev->parent->usb,
  703. usb_sndbulkpipe(odev->parent->usb,
  704. odev->out_endp->
  705. bEndpointAddress & 0x7F),
  706. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  707. odev);
  708. /* Deal with the Zero Length packet problem, I hope */
  709. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  710. /* Send the URB on its merry way. */
  711. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  712. if (result) {
  713. dev_warn(&odev->parent->interface->dev,
  714. "failed mux_bulk_tx_urb %d\n", result);
  715. net->stats.tx_errors++;
  716. netif_start_queue(net);
  717. } else {
  718. net->stats.tx_packets++;
  719. net->stats.tx_bytes += skb->len;
  720. }
  721. dev_kfree_skb(skb);
  722. /* we're done */
  723. return NETDEV_TX_OK;
  724. }
  725. static const struct ethtool_ops ops = {
  726. .get_link = ethtool_op_get_link
  727. };
  728. /* called when a packet did not ack after watchdogtimeout */
  729. static void hso_net_tx_timeout(struct net_device *net)
  730. {
  731. struct hso_net *odev = netdev_priv(net);
  732. if (!odev)
  733. return;
  734. /* Tell syslog we are hosed. */
  735. dev_warn(&net->dev, "Tx timed out.\n");
  736. /* Tear the waiting frame off the list */
  737. if (odev->mux_bulk_tx_urb &&
  738. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  739. usb_unlink_urb(odev->mux_bulk_tx_urb);
  740. /* Update statistics */
  741. net->stats.tx_errors++;
  742. }
  743. /* make a real packet from the received USB buffer */
  744. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  745. unsigned int count, unsigned char is_eop)
  746. {
  747. unsigned short temp_bytes;
  748. unsigned short buffer_offset = 0;
  749. unsigned short frame_len;
  750. unsigned char *tmp_rx_buf;
  751. /* log if needed */
  752. D1("Rx %d bytes", count);
  753. DUMP(ip_pkt, min(128, (int)count));
  754. while (count) {
  755. switch (odev->rx_parse_state) {
  756. case WAIT_IP:
  757. /* waiting for IP header. */
  758. /* wanted bytes - size of ip header */
  759. temp_bytes =
  760. (count <
  761. odev->rx_buf_missing) ? count : odev->
  762. rx_buf_missing;
  763. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  764. odev->rx_buf_size, ip_pkt + buffer_offset,
  765. temp_bytes);
  766. odev->rx_buf_size += temp_bytes;
  767. buffer_offset += temp_bytes;
  768. odev->rx_buf_missing -= temp_bytes;
  769. count -= temp_bytes;
  770. if (!odev->rx_buf_missing) {
  771. /* header is complete allocate an sk_buffer and
  772. * continue to WAIT_DATA */
  773. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  774. if ((frame_len > DEFAULT_MRU) ||
  775. (frame_len < sizeof(struct iphdr))) {
  776. dev_err(&odev->net->dev,
  777. "Invalid frame (%d) length\n",
  778. frame_len);
  779. odev->rx_parse_state = WAIT_SYNC;
  780. continue;
  781. }
  782. /* Allocate an sk_buff */
  783. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  784. frame_len);
  785. if (!odev->skb_rx_buf) {
  786. /* We got no receive buffer. */
  787. D1("could not allocate memory");
  788. odev->rx_parse_state = WAIT_SYNC;
  789. return;
  790. }
  791. /* Copy what we got so far. make room for iphdr
  792. * after tail. */
  793. tmp_rx_buf =
  794. skb_put(odev->skb_rx_buf,
  795. sizeof(struct iphdr));
  796. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  797. sizeof(struct iphdr));
  798. /* ETH_HLEN */
  799. odev->rx_buf_size = sizeof(struct iphdr);
  800. /* Filip actually use .tot_len */
  801. odev->rx_buf_missing =
  802. frame_len - sizeof(struct iphdr);
  803. odev->rx_parse_state = WAIT_DATA;
  804. }
  805. break;
  806. case WAIT_DATA:
  807. temp_bytes = (count < odev->rx_buf_missing)
  808. ? count : odev->rx_buf_missing;
  809. /* Copy the rest of the bytes that are left in the
  810. * buffer into the waiting sk_buf. */
  811. /* Make room for temp_bytes after tail. */
  812. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  813. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  814. odev->rx_buf_missing -= temp_bytes;
  815. count -= temp_bytes;
  816. buffer_offset += temp_bytes;
  817. odev->rx_buf_size += temp_bytes;
  818. if (!odev->rx_buf_missing) {
  819. /* Packet is complete. Inject into stack. */
  820. /* We have IP packet here */
  821. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  822. skb_reset_mac_header(odev->skb_rx_buf);
  823. /* Ship it off to the kernel */
  824. netif_rx(odev->skb_rx_buf);
  825. /* No longer our buffer. */
  826. odev->skb_rx_buf = NULL;
  827. /* update out statistics */
  828. odev->net->stats.rx_packets++;
  829. odev->net->stats.rx_bytes += odev->rx_buf_size;
  830. odev->rx_buf_size = 0;
  831. odev->rx_buf_missing = sizeof(struct iphdr);
  832. odev->rx_parse_state = WAIT_IP;
  833. }
  834. break;
  835. case WAIT_SYNC:
  836. D1(" W_S");
  837. count = 0;
  838. break;
  839. default:
  840. D1(" ");
  841. count--;
  842. break;
  843. }
  844. }
  845. /* Recovery mechanism for WAIT_SYNC state. */
  846. if (is_eop) {
  847. if (odev->rx_parse_state == WAIT_SYNC) {
  848. odev->rx_parse_state = WAIT_IP;
  849. odev->rx_buf_size = 0;
  850. odev->rx_buf_missing = sizeof(struct iphdr);
  851. }
  852. }
  853. }
  854. static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
  855. {
  856. static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  857. u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
  858. if (((rest == 5) || (rest == 6)) &&
  859. !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
  860. crc_check, 4)) {
  861. urb->actual_length -= 4;
  862. }
  863. }
  864. /* Moving data from usb to kernel (in interrupt state) */
  865. static void read_bulk_callback(struct urb *urb)
  866. {
  867. struct hso_net *odev = urb->context;
  868. struct net_device *net;
  869. int result;
  870. int status = urb->status;
  871. /* is al ok? (Filip: Who's Al ?) */
  872. if (status) {
  873. handle_usb_error(status, __func__, odev->parent);
  874. return;
  875. }
  876. /* Sanity check */
  877. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  878. D1("BULK IN callback but driver is not active!");
  879. return;
  880. }
  881. usb_mark_last_busy(urb->dev);
  882. net = odev->net;
  883. if (!netif_device_present(net)) {
  884. /* Somebody killed our network interface... */
  885. return;
  886. }
  887. if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
  888. fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
  889. /* do we even have a packet? */
  890. if (urb->actual_length) {
  891. /* Handle the IP stream, add header and push it onto network
  892. * stack if the packet is complete. */
  893. spin_lock(&odev->net_lock);
  894. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  895. (urb->transfer_buffer_length >
  896. urb->actual_length) ? 1 : 0);
  897. spin_unlock(&odev->net_lock);
  898. }
  899. /* We are done with this URB, resubmit it. Prep the USB to wait for
  900. * another frame. Reuse same as received. */
  901. usb_fill_bulk_urb(urb,
  902. odev->parent->usb,
  903. usb_rcvbulkpipe(odev->parent->usb,
  904. odev->in_endp->
  905. bEndpointAddress & 0x7F),
  906. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  907. read_bulk_callback, odev);
  908. /* Give this to the USB subsystem so it can tell us when more data
  909. * arrives. */
  910. result = usb_submit_urb(urb, GFP_ATOMIC);
  911. if (result)
  912. dev_warn(&odev->parent->interface->dev,
  913. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  914. result);
  915. }
  916. /* Serial driver functions */
  917. static void hso_init_termios(struct ktermios *termios)
  918. {
  919. /*
  920. * The default requirements for this device are:
  921. */
  922. termios->c_iflag &=
  923. ~(IGNBRK /* disable ignore break */
  924. | BRKINT /* disable break causes interrupt */
  925. | PARMRK /* disable mark parity errors */
  926. | ISTRIP /* disable clear high bit of input characters */
  927. | INLCR /* disable translate NL to CR */
  928. | IGNCR /* disable ignore CR */
  929. | ICRNL /* disable translate CR to NL */
  930. | IXON); /* disable enable XON/XOFF flow control */
  931. /* disable postprocess output characters */
  932. termios->c_oflag &= ~OPOST;
  933. termios->c_lflag &=
  934. ~(ECHO /* disable echo input characters */
  935. | ECHONL /* disable echo new line */
  936. | ICANON /* disable erase, kill, werase, and rprnt
  937. special characters */
  938. | ISIG /* disable interrupt, quit, and suspend special
  939. characters */
  940. | IEXTEN); /* disable non-POSIX special characters */
  941. termios->c_cflag &=
  942. ~(CSIZE /* no size */
  943. | PARENB /* disable parity bit */
  944. | CBAUD /* clear current baud rate */
  945. | CBAUDEX); /* clear current buad rate */
  946. termios->c_cflag |= CS8; /* character size 8 bits */
  947. /* baud rate 115200 */
  948. tty_termios_encode_baud_rate(termios, 115200, 115200);
  949. }
  950. static void _hso_serial_set_termios(struct tty_struct *tty,
  951. struct ktermios *old)
  952. {
  953. struct hso_serial *serial = tty->driver_data;
  954. if (!serial) {
  955. printk(KERN_ERR "%s: no tty structures", __func__);
  956. return;
  957. }
  958. D4("port %d", serial->minor);
  959. /*
  960. * Fix up unsupported bits
  961. */
  962. tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  963. tty->termios.c_cflag &=
  964. ~(CSIZE /* no size */
  965. | PARENB /* disable parity bit */
  966. | CBAUD /* clear current baud rate */
  967. | CBAUDEX); /* clear current buad rate */
  968. tty->termios.c_cflag |= CS8; /* character size 8 bits */
  969. /* baud rate 115200 */
  970. tty_encode_baud_rate(tty, 115200, 115200);
  971. }
  972. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  973. {
  974. int result;
  975. /* We are done with this URB, resubmit it. Prep the USB to wait for
  976. * another frame */
  977. usb_fill_bulk_urb(urb, serial->parent->usb,
  978. usb_rcvbulkpipe(serial->parent->usb,
  979. serial->in_endp->
  980. bEndpointAddress & 0x7F),
  981. urb->transfer_buffer, serial->rx_data_length,
  982. hso_std_serial_read_bulk_callback, serial);
  983. /* Give this to the USB subsystem so it can tell us when more data
  984. * arrives. */
  985. result = usb_submit_urb(urb, GFP_ATOMIC);
  986. if (result) {
  987. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  988. __func__, result);
  989. }
  990. }
  991. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  992. {
  993. int count;
  994. struct urb *curr_urb;
  995. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  996. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  997. count = put_rxbuf_data(curr_urb, serial);
  998. if (count == -1)
  999. return;
  1000. if (count == 0) {
  1001. serial->curr_rx_urb_idx++;
  1002. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1003. serial->curr_rx_urb_idx = 0;
  1004. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1005. }
  1006. }
  1007. }
  1008. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1009. {
  1010. int count = 0;
  1011. struct urb *urb;
  1012. urb = serial->rx_urb[0];
  1013. if (serial->port.count > 0) {
  1014. count = put_rxbuf_data(urb, serial);
  1015. if (count == -1)
  1016. return;
  1017. }
  1018. /* Re issue a read as long as we receive data. */
  1019. if (count == 0 && ((urb->actual_length != 0) ||
  1020. (serial->rx_state == RX_PENDING))) {
  1021. serial->rx_state = RX_SENT;
  1022. hso_mux_serial_read(serial);
  1023. } else
  1024. serial->rx_state = RX_IDLE;
  1025. }
  1026. /* read callback for Diag and CS port */
  1027. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1028. {
  1029. struct hso_serial *serial = urb->context;
  1030. int status = urb->status;
  1031. /* sanity check */
  1032. if (!serial) {
  1033. D1("serial == NULL");
  1034. return;
  1035. } else if (status) {
  1036. handle_usb_error(status, __func__, serial->parent);
  1037. return;
  1038. }
  1039. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1040. D1("Actual length = %d\n", urb->actual_length);
  1041. DUMP1(urb->transfer_buffer, urb->actual_length);
  1042. /* Anyone listening? */
  1043. if (serial->port.count == 0)
  1044. return;
  1045. if (status == 0) {
  1046. if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
  1047. fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
  1048. /* Valid data, handle RX data */
  1049. spin_lock(&serial->serial_lock);
  1050. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1051. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1052. spin_unlock(&serial->serial_lock);
  1053. } else if (status == -ENOENT || status == -ECONNRESET) {
  1054. /* Unlinked - check for throttled port. */
  1055. D2("Port %d, successfully unlinked urb", serial->minor);
  1056. spin_lock(&serial->serial_lock);
  1057. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1058. hso_resubmit_rx_bulk_urb(serial, urb);
  1059. spin_unlock(&serial->serial_lock);
  1060. } else {
  1061. D2("Port %d, status = %d for read urb", serial->minor, status);
  1062. return;
  1063. }
  1064. }
  1065. /*
  1066. * This needs to be a tasklet otherwise we will
  1067. * end up recursively calling this function.
  1068. */
  1069. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1070. {
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&serial->serial_lock, flags);
  1073. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1074. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1075. else
  1076. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1077. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1078. }
  1079. static void hso_unthrottle(struct tty_struct *tty)
  1080. {
  1081. struct hso_serial *serial = tty->driver_data;
  1082. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1083. }
  1084. static void hso_unthrottle_workfunc(struct work_struct *work)
  1085. {
  1086. struct hso_serial *serial =
  1087. container_of(work, struct hso_serial,
  1088. retry_unthrottle_workqueue);
  1089. hso_unthrottle_tasklet(serial);
  1090. }
  1091. /* open the requested serial port */
  1092. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1093. {
  1094. struct hso_serial *serial = get_serial_by_index(tty->index);
  1095. int result;
  1096. /* sanity check */
  1097. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1098. WARN_ON(1);
  1099. tty->driver_data = NULL;
  1100. D1("Failed to open port");
  1101. return -ENODEV;
  1102. }
  1103. mutex_lock(&serial->parent->mutex);
  1104. result = usb_autopm_get_interface(serial->parent->interface);
  1105. if (result < 0)
  1106. goto err_out;
  1107. D1("Opening %d", serial->minor);
  1108. kref_get(&serial->parent->ref);
  1109. /* setup */
  1110. tty->driver_data = serial;
  1111. tty_port_tty_set(&serial->port, tty);
  1112. /* check for port already opened, if not set the termios */
  1113. serial->port.count++;
  1114. if (serial->port.count == 1) {
  1115. serial->rx_state = RX_IDLE;
  1116. /* Force default termio settings */
  1117. _hso_serial_set_termios(tty, NULL);
  1118. tasklet_init(&serial->unthrottle_tasklet,
  1119. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1120. (unsigned long)serial);
  1121. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1122. hso_unthrottle_workfunc);
  1123. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1124. if (result) {
  1125. hso_stop_serial_device(serial->parent);
  1126. serial->port.count--;
  1127. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1128. }
  1129. } else {
  1130. D1("Port was already open");
  1131. }
  1132. usb_autopm_put_interface(serial->parent->interface);
  1133. /* done */
  1134. if (result)
  1135. hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
  1136. err_out:
  1137. mutex_unlock(&serial->parent->mutex);
  1138. return result;
  1139. }
  1140. /* close the requested serial port */
  1141. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1142. {
  1143. struct hso_serial *serial = tty->driver_data;
  1144. u8 usb_gone;
  1145. D1("Closing serial port");
  1146. /* Open failed, no close cleanup required */
  1147. if (serial == NULL)
  1148. return;
  1149. mutex_lock(&serial->parent->mutex);
  1150. usb_gone = serial->parent->usb_gone;
  1151. if (!usb_gone)
  1152. usb_autopm_get_interface(serial->parent->interface);
  1153. /* reset the rts and dtr */
  1154. /* do the actual close */
  1155. serial->port.count--;
  1156. if (serial->port.count <= 0) {
  1157. serial->port.count = 0;
  1158. tty_port_tty_set(&serial->port, NULL);
  1159. if (!usb_gone)
  1160. hso_stop_serial_device(serial->parent);
  1161. tasklet_kill(&serial->unthrottle_tasklet);
  1162. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1163. }
  1164. if (!usb_gone)
  1165. usb_autopm_put_interface(serial->parent->interface);
  1166. mutex_unlock(&serial->parent->mutex);
  1167. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1168. }
  1169. /* close the requested serial port */
  1170. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1171. int count)
  1172. {
  1173. struct hso_serial *serial = tty->driver_data;
  1174. int space, tx_bytes;
  1175. unsigned long flags;
  1176. /* sanity check */
  1177. if (serial == NULL) {
  1178. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1179. return -ENODEV;
  1180. }
  1181. spin_lock_irqsave(&serial->serial_lock, flags);
  1182. space = serial->tx_data_length - serial->tx_buffer_count;
  1183. tx_bytes = (count < space) ? count : space;
  1184. if (!tx_bytes)
  1185. goto out;
  1186. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1187. serial->tx_buffer_count += tx_bytes;
  1188. out:
  1189. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1190. hso_kick_transmit(serial);
  1191. /* done */
  1192. return tx_bytes;
  1193. }
  1194. /* how much room is there for writing */
  1195. static int hso_serial_write_room(struct tty_struct *tty)
  1196. {
  1197. struct hso_serial *serial = tty->driver_data;
  1198. int room;
  1199. unsigned long flags;
  1200. spin_lock_irqsave(&serial->serial_lock, flags);
  1201. room = serial->tx_data_length - serial->tx_buffer_count;
  1202. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1203. /* return free room */
  1204. return room;
  1205. }
  1206. /* setup the term */
  1207. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1208. {
  1209. struct hso_serial *serial = tty->driver_data;
  1210. unsigned long flags;
  1211. if (old)
  1212. D5("Termios called with: cflags new[%d] - old[%d]",
  1213. tty->termios.c_cflag, old->c_cflag);
  1214. /* the actual setup */
  1215. spin_lock_irqsave(&serial->serial_lock, flags);
  1216. if (serial->port.count)
  1217. _hso_serial_set_termios(tty, old);
  1218. else
  1219. tty->termios = *old;
  1220. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1221. /* done */
  1222. }
  1223. /* how many characters in the buffer */
  1224. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1225. {
  1226. struct hso_serial *serial = tty->driver_data;
  1227. int chars;
  1228. unsigned long flags;
  1229. /* sanity check */
  1230. if (serial == NULL)
  1231. return 0;
  1232. spin_lock_irqsave(&serial->serial_lock, flags);
  1233. chars = serial->tx_buffer_count;
  1234. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1235. return chars;
  1236. }
  1237. static int tiocmget_submit_urb(struct hso_serial *serial,
  1238. struct hso_tiocmget *tiocmget,
  1239. struct usb_device *usb)
  1240. {
  1241. int result;
  1242. if (serial->parent->usb_gone)
  1243. return -ENODEV;
  1244. usb_fill_int_urb(tiocmget->urb, usb,
  1245. usb_rcvintpipe(usb,
  1246. tiocmget->endp->
  1247. bEndpointAddress & 0x7F),
  1248. &tiocmget->serial_state_notification,
  1249. sizeof(struct hso_serial_state_notification),
  1250. tiocmget_intr_callback, serial,
  1251. tiocmget->endp->bInterval);
  1252. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1253. if (result) {
  1254. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1255. result);
  1256. }
  1257. return result;
  1258. }
  1259. static void tiocmget_intr_callback(struct urb *urb)
  1260. {
  1261. struct hso_serial *serial = urb->context;
  1262. struct hso_tiocmget *tiocmget;
  1263. int status = urb->status;
  1264. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1265. struct uart_icount *icount;
  1266. struct hso_serial_state_notification *serial_state_notification;
  1267. struct usb_device *usb;
  1268. /* Sanity checks */
  1269. if (!serial)
  1270. return;
  1271. if (status) {
  1272. handle_usb_error(status, __func__, serial->parent);
  1273. return;
  1274. }
  1275. tiocmget = serial->tiocmget;
  1276. if (!tiocmget)
  1277. return;
  1278. usb = serial->parent->usb;
  1279. serial_state_notification = &tiocmget->serial_state_notification;
  1280. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1281. serial_state_notification->bNotification != B_NOTIFICATION ||
  1282. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1283. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1284. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1285. dev_warn(&usb->dev,
  1286. "hso received invalid serial state notification\n");
  1287. DUMP(serial_state_notification,
  1288. sizeof(struct hso_serial_state_notification));
  1289. } else {
  1290. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1291. UART_state_bitmap);
  1292. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1293. icount = &tiocmget->icount;
  1294. spin_lock(&serial->serial_lock);
  1295. if ((UART_state_bitmap & B_OVERRUN) !=
  1296. (prev_UART_state_bitmap & B_OVERRUN))
  1297. icount->parity++;
  1298. if ((UART_state_bitmap & B_PARITY) !=
  1299. (prev_UART_state_bitmap & B_PARITY))
  1300. icount->parity++;
  1301. if ((UART_state_bitmap & B_FRAMING) !=
  1302. (prev_UART_state_bitmap & B_FRAMING))
  1303. icount->frame++;
  1304. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1305. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1306. icount->rng++;
  1307. if ((UART_state_bitmap & B_BREAK) !=
  1308. (prev_UART_state_bitmap & B_BREAK))
  1309. icount->brk++;
  1310. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1311. (prev_UART_state_bitmap & B_TX_CARRIER))
  1312. icount->dsr++;
  1313. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1314. (prev_UART_state_bitmap & B_RX_CARRIER))
  1315. icount->dcd++;
  1316. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1317. spin_unlock(&serial->serial_lock);
  1318. tiocmget->intr_completed = 1;
  1319. wake_up_interruptible(&tiocmget->waitq);
  1320. }
  1321. memset(serial_state_notification, 0,
  1322. sizeof(struct hso_serial_state_notification));
  1323. tiocmget_submit_urb(serial,
  1324. tiocmget,
  1325. serial->parent->usb);
  1326. }
  1327. /*
  1328. * next few functions largely stolen from drivers/serial/serial_core.c
  1329. */
  1330. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1331. * - mask passed in arg for lines of interest
  1332. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1333. * Caller should use TIOCGICOUNT to see which one it was
  1334. */
  1335. static int
  1336. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1337. {
  1338. DECLARE_WAITQUEUE(wait, current);
  1339. struct uart_icount cprev, cnow;
  1340. struct hso_tiocmget *tiocmget;
  1341. int ret;
  1342. tiocmget = serial->tiocmget;
  1343. if (!tiocmget)
  1344. return -ENOENT;
  1345. /*
  1346. * note the counters on entry
  1347. */
  1348. spin_lock_irq(&serial->serial_lock);
  1349. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1350. spin_unlock_irq(&serial->serial_lock);
  1351. add_wait_queue(&tiocmget->waitq, &wait);
  1352. for (;;) {
  1353. spin_lock_irq(&serial->serial_lock);
  1354. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1355. spin_unlock_irq(&serial->serial_lock);
  1356. set_current_state(TASK_INTERRUPTIBLE);
  1357. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1358. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1359. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1360. ret = 0;
  1361. break;
  1362. }
  1363. schedule();
  1364. /* see if a signal did it */
  1365. if (signal_pending(current)) {
  1366. ret = -ERESTARTSYS;
  1367. break;
  1368. }
  1369. cprev = cnow;
  1370. }
  1371. current->state = TASK_RUNNING;
  1372. remove_wait_queue(&tiocmget->waitq, &wait);
  1373. return ret;
  1374. }
  1375. /*
  1376. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1377. * Return: write counters to the user passed counter struct
  1378. * NB: both 1->0 and 0->1 transitions are counted except for
  1379. * RI where only 0->1 is counted.
  1380. */
  1381. static int hso_get_count(struct tty_struct *tty,
  1382. struct serial_icounter_struct *icount)
  1383. {
  1384. struct uart_icount cnow;
  1385. struct hso_serial *serial = tty->driver_data;
  1386. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1387. memset(icount, 0, sizeof(struct serial_icounter_struct));
  1388. if (!tiocmget)
  1389. return -ENOENT;
  1390. spin_lock_irq(&serial->serial_lock);
  1391. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1392. spin_unlock_irq(&serial->serial_lock);
  1393. icount->cts = cnow.cts;
  1394. icount->dsr = cnow.dsr;
  1395. icount->rng = cnow.rng;
  1396. icount->dcd = cnow.dcd;
  1397. icount->rx = cnow.rx;
  1398. icount->tx = cnow.tx;
  1399. icount->frame = cnow.frame;
  1400. icount->overrun = cnow.overrun;
  1401. icount->parity = cnow.parity;
  1402. icount->brk = cnow.brk;
  1403. icount->buf_overrun = cnow.buf_overrun;
  1404. return 0;
  1405. }
  1406. static int hso_serial_tiocmget(struct tty_struct *tty)
  1407. {
  1408. int retval;
  1409. struct hso_serial *serial = tty->driver_data;
  1410. struct hso_tiocmget *tiocmget;
  1411. u16 UART_state_bitmap;
  1412. /* sanity check */
  1413. if (!serial) {
  1414. D1("no tty structures");
  1415. return -EINVAL;
  1416. }
  1417. spin_lock_irq(&serial->serial_lock);
  1418. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1419. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1420. tiocmget = serial->tiocmget;
  1421. if (tiocmget) {
  1422. UART_state_bitmap = le16_to_cpu(
  1423. tiocmget->prev_UART_state_bitmap);
  1424. if (UART_state_bitmap & B_RING_SIGNAL)
  1425. retval |= TIOCM_RNG;
  1426. if (UART_state_bitmap & B_RX_CARRIER)
  1427. retval |= TIOCM_CD;
  1428. if (UART_state_bitmap & B_TX_CARRIER)
  1429. retval |= TIOCM_DSR;
  1430. }
  1431. spin_unlock_irq(&serial->serial_lock);
  1432. return retval;
  1433. }
  1434. static int hso_serial_tiocmset(struct tty_struct *tty,
  1435. unsigned int set, unsigned int clear)
  1436. {
  1437. int val = 0;
  1438. unsigned long flags;
  1439. int if_num;
  1440. struct hso_serial *serial = tty->driver_data;
  1441. /* sanity check */
  1442. if (!serial) {
  1443. D1("no tty structures");
  1444. return -EINVAL;
  1445. }
  1446. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1447. return -EINVAL;
  1448. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1449. spin_lock_irqsave(&serial->serial_lock, flags);
  1450. if (set & TIOCM_RTS)
  1451. serial->rts_state = 1;
  1452. if (set & TIOCM_DTR)
  1453. serial->dtr_state = 1;
  1454. if (clear & TIOCM_RTS)
  1455. serial->rts_state = 0;
  1456. if (clear & TIOCM_DTR)
  1457. serial->dtr_state = 0;
  1458. if (serial->dtr_state)
  1459. val |= 0x01;
  1460. if (serial->rts_state)
  1461. val |= 0x02;
  1462. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1463. return usb_control_msg(serial->parent->usb,
  1464. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1465. 0x21, val, if_num, NULL, 0,
  1466. USB_CTRL_SET_TIMEOUT);
  1467. }
  1468. static int hso_serial_ioctl(struct tty_struct *tty,
  1469. unsigned int cmd, unsigned long arg)
  1470. {
  1471. struct hso_serial *serial = tty->driver_data;
  1472. int ret = 0;
  1473. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1474. if (!serial)
  1475. return -ENODEV;
  1476. switch (cmd) {
  1477. case TIOCMIWAIT:
  1478. ret = hso_wait_modem_status(serial, arg);
  1479. break;
  1480. default:
  1481. ret = -ENOIOCTLCMD;
  1482. break;
  1483. }
  1484. return ret;
  1485. }
  1486. /* starts a transmit */
  1487. static void hso_kick_transmit(struct hso_serial *serial)
  1488. {
  1489. u8 *temp;
  1490. unsigned long flags;
  1491. int res;
  1492. spin_lock_irqsave(&serial->serial_lock, flags);
  1493. if (!serial->tx_buffer_count)
  1494. goto out;
  1495. if (serial->tx_urb_used)
  1496. goto out;
  1497. /* Wakeup USB interface if necessary */
  1498. if (hso_get_activity(serial->parent) == -EAGAIN)
  1499. goto out;
  1500. /* Switch pointers around to avoid memcpy */
  1501. temp = serial->tx_buffer;
  1502. serial->tx_buffer = serial->tx_data;
  1503. serial->tx_data = temp;
  1504. serial->tx_data_count = serial->tx_buffer_count;
  1505. serial->tx_buffer_count = 0;
  1506. /* If temp is set, it means we switched buffers */
  1507. if (temp && serial->write_data) {
  1508. res = serial->write_data(serial);
  1509. if (res >= 0)
  1510. serial->tx_urb_used = 1;
  1511. }
  1512. out:
  1513. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1514. }
  1515. /* make a request (for reading and writing data to muxed serial port) */
  1516. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1517. struct urb *ctrl_urb,
  1518. struct usb_ctrlrequest *ctrl_req,
  1519. u8 *ctrl_urb_data, u32 size)
  1520. {
  1521. int result;
  1522. int pipe;
  1523. /* Sanity check */
  1524. if (!serial || !ctrl_urb || !ctrl_req) {
  1525. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1526. return -EINVAL;
  1527. }
  1528. /* initialize */
  1529. ctrl_req->wValue = 0;
  1530. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1531. ctrl_req->wLength = cpu_to_le16(size);
  1532. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1533. /* Reading command */
  1534. ctrl_req->bRequestType = USB_DIR_IN |
  1535. USB_TYPE_OPTION_VENDOR |
  1536. USB_RECIP_INTERFACE;
  1537. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1538. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1539. } else {
  1540. /* Writing command */
  1541. ctrl_req->bRequestType = USB_DIR_OUT |
  1542. USB_TYPE_OPTION_VENDOR |
  1543. USB_RECIP_INTERFACE;
  1544. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1545. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1546. }
  1547. /* syslog */
  1548. D2("%s command (%02x) len: %d, port: %d",
  1549. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1550. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1551. /* Load ctrl urb */
  1552. ctrl_urb->transfer_flags = 0;
  1553. usb_fill_control_urb(ctrl_urb,
  1554. serial->parent->usb,
  1555. pipe,
  1556. (u8 *) ctrl_req,
  1557. ctrl_urb_data, size, ctrl_callback, serial);
  1558. /* Send it on merry way */
  1559. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1560. if (result) {
  1561. dev_err(&ctrl_urb->dev->dev,
  1562. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1563. result, type);
  1564. return result;
  1565. }
  1566. /* done */
  1567. return size;
  1568. }
  1569. /* called by intr_callback when read occurs */
  1570. static int hso_mux_serial_read(struct hso_serial *serial)
  1571. {
  1572. if (!serial)
  1573. return -EINVAL;
  1574. /* clean data */
  1575. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1576. /* make the request */
  1577. if (serial->num_rx_urbs != 1) {
  1578. dev_err(&serial->parent->interface->dev,
  1579. "ERROR: mux'd reads with multiple buffers "
  1580. "not possible\n");
  1581. return 0;
  1582. }
  1583. return mux_device_request(serial,
  1584. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1585. serial->parent->port_spec & HSO_PORT_MASK,
  1586. serial->rx_urb[0],
  1587. &serial->ctrl_req_rx,
  1588. serial->rx_data[0], serial->rx_data_length);
  1589. }
  1590. /* used for muxed serial port callback (muxed serial read) */
  1591. static void intr_callback(struct urb *urb)
  1592. {
  1593. struct hso_shared_int *shared_int = urb->context;
  1594. struct hso_serial *serial;
  1595. unsigned char *port_req;
  1596. int status = urb->status;
  1597. int i;
  1598. usb_mark_last_busy(urb->dev);
  1599. /* sanity check */
  1600. if (!shared_int)
  1601. return;
  1602. /* status check */
  1603. if (status) {
  1604. handle_usb_error(status, __func__, NULL);
  1605. return;
  1606. }
  1607. D4("\n--- Got intr callback 0x%02X ---", status);
  1608. /* what request? */
  1609. port_req = urb->transfer_buffer;
  1610. D4(" port_req = 0x%.2X\n", *port_req);
  1611. /* loop over all muxed ports to find the one sending this */
  1612. for (i = 0; i < 8; i++) {
  1613. /* max 8 channels on MUX */
  1614. if (*port_req & (1 << i)) {
  1615. serial = get_serial_by_shared_int_and_type(shared_int,
  1616. (1 << i));
  1617. if (serial != NULL) {
  1618. D1("Pending read interrupt on port %d\n", i);
  1619. spin_lock(&serial->serial_lock);
  1620. if (serial->rx_state == RX_IDLE &&
  1621. serial->port.count > 0) {
  1622. /* Setup and send a ctrl req read on
  1623. * port i */
  1624. if (!serial->rx_urb_filled[0]) {
  1625. serial->rx_state = RX_SENT;
  1626. hso_mux_serial_read(serial);
  1627. } else
  1628. serial->rx_state = RX_PENDING;
  1629. } else {
  1630. D1("Already a read pending on "
  1631. "port %d or port not open\n", i);
  1632. }
  1633. spin_unlock(&serial->serial_lock);
  1634. }
  1635. }
  1636. }
  1637. /* Resubmit interrupt urb */
  1638. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1639. }
  1640. /* called for writing to muxed serial port */
  1641. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1642. {
  1643. if (NULL == serial)
  1644. return -EINVAL;
  1645. return mux_device_request(serial,
  1646. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1647. serial->parent->port_spec & HSO_PORT_MASK,
  1648. serial->tx_urb,
  1649. &serial->ctrl_req_tx,
  1650. serial->tx_data, serial->tx_data_count);
  1651. }
  1652. /* write callback for Diag and CS port */
  1653. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1654. {
  1655. struct hso_serial *serial = urb->context;
  1656. int status = urb->status;
  1657. struct tty_struct *tty;
  1658. /* sanity check */
  1659. if (!serial) {
  1660. D1("serial == NULL");
  1661. return;
  1662. }
  1663. spin_lock(&serial->serial_lock);
  1664. serial->tx_urb_used = 0;
  1665. spin_unlock(&serial->serial_lock);
  1666. if (status) {
  1667. handle_usb_error(status, __func__, serial->parent);
  1668. return;
  1669. }
  1670. hso_put_activity(serial->parent);
  1671. tty = tty_port_tty_get(&serial->port);
  1672. if (tty) {
  1673. tty_wakeup(tty);
  1674. tty_kref_put(tty);
  1675. }
  1676. hso_kick_transmit(serial);
  1677. D1(" ");
  1678. }
  1679. /* called for writing diag or CS serial port */
  1680. static int hso_std_serial_write_data(struct hso_serial *serial)
  1681. {
  1682. int count = serial->tx_data_count;
  1683. int result;
  1684. usb_fill_bulk_urb(serial->tx_urb,
  1685. serial->parent->usb,
  1686. usb_sndbulkpipe(serial->parent->usb,
  1687. serial->out_endp->
  1688. bEndpointAddress & 0x7F),
  1689. serial->tx_data, serial->tx_data_count,
  1690. hso_std_serial_write_bulk_callback, serial);
  1691. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1692. if (result) {
  1693. dev_warn(&serial->parent->usb->dev,
  1694. "Failed to submit urb - res %d\n", result);
  1695. return result;
  1696. }
  1697. return count;
  1698. }
  1699. /* callback after read or write on muxed serial port */
  1700. static void ctrl_callback(struct urb *urb)
  1701. {
  1702. struct hso_serial *serial = urb->context;
  1703. struct usb_ctrlrequest *req;
  1704. int status = urb->status;
  1705. /* sanity check */
  1706. if (!serial)
  1707. return;
  1708. spin_lock(&serial->serial_lock);
  1709. serial->tx_urb_used = 0;
  1710. spin_unlock(&serial->serial_lock);
  1711. if (status) {
  1712. handle_usb_error(status, __func__, serial->parent);
  1713. return;
  1714. }
  1715. /* what request? */
  1716. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1717. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1718. D4("Actual length of urb = %d\n", urb->actual_length);
  1719. DUMP1(urb->transfer_buffer, urb->actual_length);
  1720. if (req->bRequestType ==
  1721. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1722. /* response to a read command */
  1723. serial->rx_urb_filled[0] = 1;
  1724. spin_lock(&serial->serial_lock);
  1725. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1726. spin_unlock(&serial->serial_lock);
  1727. } else {
  1728. struct tty_struct *tty = tty_port_tty_get(&serial->port);
  1729. hso_put_activity(serial->parent);
  1730. if (tty) {
  1731. tty_wakeup(tty);
  1732. tty_kref_put(tty);
  1733. }
  1734. /* response to a write command */
  1735. hso_kick_transmit(serial);
  1736. }
  1737. }
  1738. /* handle RX data for serial port */
  1739. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1740. {
  1741. struct tty_struct *tty;
  1742. int write_length_remaining = 0;
  1743. int curr_write_len;
  1744. /* Sanity check */
  1745. if (urb == NULL || serial == NULL) {
  1746. D1("serial = NULL");
  1747. return -2;
  1748. }
  1749. tty = tty_port_tty_get(&serial->port);
  1750. /* Push data to tty */
  1751. if (tty) {
  1752. write_length_remaining = urb->actual_length -
  1753. serial->curr_rx_urb_offset;
  1754. D1("data to push to tty");
  1755. while (write_length_remaining) {
  1756. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1757. tty_kref_put(tty);
  1758. return -1;
  1759. }
  1760. curr_write_len = tty_insert_flip_string
  1761. (tty, urb->transfer_buffer +
  1762. serial->curr_rx_urb_offset,
  1763. write_length_remaining);
  1764. serial->curr_rx_urb_offset += curr_write_len;
  1765. write_length_remaining -= curr_write_len;
  1766. tty_flip_buffer_push(tty);
  1767. }
  1768. tty_kref_put(tty);
  1769. }
  1770. if (write_length_remaining == 0) {
  1771. serial->curr_rx_urb_offset = 0;
  1772. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1773. }
  1774. return write_length_remaining;
  1775. }
  1776. /* Base driver functions */
  1777. static void hso_log_port(struct hso_device *hso_dev)
  1778. {
  1779. char *port_type;
  1780. char port_dev[20];
  1781. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1782. case HSO_PORT_CONTROL:
  1783. port_type = "Control";
  1784. break;
  1785. case HSO_PORT_APP:
  1786. port_type = "Application";
  1787. break;
  1788. case HSO_PORT_GPS:
  1789. port_type = "GPS";
  1790. break;
  1791. case HSO_PORT_GPS_CONTROL:
  1792. port_type = "GPS control";
  1793. break;
  1794. case HSO_PORT_APP2:
  1795. port_type = "Application2";
  1796. break;
  1797. case HSO_PORT_PCSC:
  1798. port_type = "PCSC";
  1799. break;
  1800. case HSO_PORT_DIAG:
  1801. port_type = "Diagnostic";
  1802. break;
  1803. case HSO_PORT_DIAG2:
  1804. port_type = "Diagnostic2";
  1805. break;
  1806. case HSO_PORT_MODEM:
  1807. port_type = "Modem";
  1808. break;
  1809. case HSO_PORT_NETWORK:
  1810. port_type = "Network";
  1811. break;
  1812. default:
  1813. port_type = "Unknown";
  1814. break;
  1815. }
  1816. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1817. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1818. } else
  1819. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1820. dev2ser(hso_dev)->minor);
  1821. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1822. port_type, port_dev);
  1823. }
  1824. static int hso_start_net_device(struct hso_device *hso_dev)
  1825. {
  1826. int i, result = 0;
  1827. struct hso_net *hso_net = dev2net(hso_dev);
  1828. if (!hso_net)
  1829. return -ENODEV;
  1830. /* send URBs for all read buffers */
  1831. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1832. /* Prep a receive URB */
  1833. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1834. hso_dev->usb,
  1835. usb_rcvbulkpipe(hso_dev->usb,
  1836. hso_net->in_endp->
  1837. bEndpointAddress & 0x7F),
  1838. hso_net->mux_bulk_rx_buf_pool[i],
  1839. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1840. hso_net);
  1841. /* Put it out there so the device can send us stuff */
  1842. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1843. GFP_NOIO);
  1844. if (result)
  1845. dev_warn(&hso_dev->usb->dev,
  1846. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1847. i, result);
  1848. }
  1849. return result;
  1850. }
  1851. static int hso_stop_net_device(struct hso_device *hso_dev)
  1852. {
  1853. int i;
  1854. struct hso_net *hso_net = dev2net(hso_dev);
  1855. if (!hso_net)
  1856. return -ENODEV;
  1857. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1858. if (hso_net->mux_bulk_rx_urb_pool[i])
  1859. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1860. }
  1861. if (hso_net->mux_bulk_tx_urb)
  1862. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1863. return 0;
  1864. }
  1865. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1866. {
  1867. int i, result = 0;
  1868. struct hso_serial *serial = dev2ser(hso_dev);
  1869. if (!serial)
  1870. return -ENODEV;
  1871. /* If it is not the MUX port fill in and submit a bulk urb (already
  1872. * allocated in hso_serial_start) */
  1873. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1874. for (i = 0; i < serial->num_rx_urbs; i++) {
  1875. usb_fill_bulk_urb(serial->rx_urb[i],
  1876. serial->parent->usb,
  1877. usb_rcvbulkpipe(serial->parent->usb,
  1878. serial->in_endp->
  1879. bEndpointAddress &
  1880. 0x7F),
  1881. serial->rx_data[i],
  1882. serial->rx_data_length,
  1883. hso_std_serial_read_bulk_callback,
  1884. serial);
  1885. result = usb_submit_urb(serial->rx_urb[i], flags);
  1886. if (result) {
  1887. dev_warn(&serial->parent->usb->dev,
  1888. "Failed to submit urb - res %d\n",
  1889. result);
  1890. break;
  1891. }
  1892. }
  1893. } else {
  1894. mutex_lock(&serial->shared_int->shared_int_lock);
  1895. if (!serial->shared_int->use_count) {
  1896. result =
  1897. hso_mux_submit_intr_urb(serial->shared_int,
  1898. hso_dev->usb, flags);
  1899. }
  1900. serial->shared_int->use_count++;
  1901. mutex_unlock(&serial->shared_int->shared_int_lock);
  1902. }
  1903. if (serial->tiocmget)
  1904. tiocmget_submit_urb(serial,
  1905. serial->tiocmget,
  1906. serial->parent->usb);
  1907. return result;
  1908. }
  1909. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1910. {
  1911. int i;
  1912. struct hso_serial *serial = dev2ser(hso_dev);
  1913. struct hso_tiocmget *tiocmget;
  1914. if (!serial)
  1915. return -ENODEV;
  1916. for (i = 0; i < serial->num_rx_urbs; i++) {
  1917. if (serial->rx_urb[i]) {
  1918. usb_kill_urb(serial->rx_urb[i]);
  1919. serial->rx_urb_filled[i] = 0;
  1920. }
  1921. }
  1922. serial->curr_rx_urb_idx = 0;
  1923. serial->curr_rx_urb_offset = 0;
  1924. if (serial->tx_urb)
  1925. usb_kill_urb(serial->tx_urb);
  1926. if (serial->shared_int) {
  1927. mutex_lock(&serial->shared_int->shared_int_lock);
  1928. if (serial->shared_int->use_count &&
  1929. (--serial->shared_int->use_count == 0)) {
  1930. struct urb *urb;
  1931. urb = serial->shared_int->shared_intr_urb;
  1932. if (urb)
  1933. usb_kill_urb(urb);
  1934. }
  1935. mutex_unlock(&serial->shared_int->shared_int_lock);
  1936. }
  1937. tiocmget = serial->tiocmget;
  1938. if (tiocmget) {
  1939. wake_up_interruptible(&tiocmget->waitq);
  1940. usb_kill_urb(tiocmget->urb);
  1941. }
  1942. return 0;
  1943. }
  1944. static void hso_serial_common_free(struct hso_serial *serial)
  1945. {
  1946. int i;
  1947. if (serial->parent->dev)
  1948. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1949. tty_unregister_device(tty_drv, serial->minor);
  1950. for (i = 0; i < serial->num_rx_urbs; i++) {
  1951. /* unlink and free RX URB */
  1952. usb_free_urb(serial->rx_urb[i]);
  1953. /* free the RX buffer */
  1954. kfree(serial->rx_data[i]);
  1955. }
  1956. /* unlink and free TX URB */
  1957. usb_free_urb(serial->tx_urb);
  1958. kfree(serial->tx_data);
  1959. tty_port_destroy(&serial->port);
  1960. }
  1961. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1962. int rx_size, int tx_size)
  1963. {
  1964. struct device *dev;
  1965. int minor;
  1966. int i;
  1967. tty_port_init(&serial->port);
  1968. minor = get_free_serial_index();
  1969. if (minor < 0)
  1970. goto exit;
  1971. /* register our minor number */
  1972. serial->parent->dev = tty_port_register_device(&serial->port, tty_drv,
  1973. minor, &serial->parent->interface->dev);
  1974. dev = serial->parent->dev;
  1975. dev_set_drvdata(dev, serial->parent);
  1976. i = device_create_file(dev, &dev_attr_hsotype);
  1977. /* fill in specific data for later use */
  1978. serial->minor = minor;
  1979. serial->magic = HSO_SERIAL_MAGIC;
  1980. spin_lock_init(&serial->serial_lock);
  1981. serial->num_rx_urbs = num_urbs;
  1982. /* RX, allocate urb and initialize */
  1983. /* prepare our RX buffer */
  1984. serial->rx_data_length = rx_size;
  1985. for (i = 0; i < serial->num_rx_urbs; i++) {
  1986. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1987. if (!serial->rx_urb[i]) {
  1988. dev_err(dev, "Could not allocate urb?\n");
  1989. goto exit;
  1990. }
  1991. serial->rx_urb[i]->transfer_buffer = NULL;
  1992. serial->rx_urb[i]->transfer_buffer_length = 0;
  1993. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1994. GFP_KERNEL);
  1995. if (!serial->rx_data[i])
  1996. goto exit;
  1997. }
  1998. /* TX, allocate urb and initialize */
  1999. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2000. if (!serial->tx_urb) {
  2001. dev_err(dev, "Could not allocate urb?\n");
  2002. goto exit;
  2003. }
  2004. serial->tx_urb->transfer_buffer = NULL;
  2005. serial->tx_urb->transfer_buffer_length = 0;
  2006. /* prepare our TX buffer */
  2007. serial->tx_data_count = 0;
  2008. serial->tx_buffer_count = 0;
  2009. serial->tx_data_length = tx_size;
  2010. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2011. if (!serial->tx_data)
  2012. goto exit;
  2013. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2014. if (!serial->tx_buffer)
  2015. goto exit;
  2016. return 0;
  2017. exit:
  2018. hso_serial_common_free(serial);
  2019. return -1;
  2020. }
  2021. /* Creates a general hso device */
  2022. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2023. int port_spec)
  2024. {
  2025. struct hso_device *hso_dev;
  2026. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2027. if (!hso_dev)
  2028. return NULL;
  2029. hso_dev->port_spec = port_spec;
  2030. hso_dev->usb = interface_to_usbdev(intf);
  2031. hso_dev->interface = intf;
  2032. kref_init(&hso_dev->ref);
  2033. mutex_init(&hso_dev->mutex);
  2034. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2035. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2036. INIT_WORK(&hso_dev->reset_device, reset_device);
  2037. return hso_dev;
  2038. }
  2039. /* Removes a network device in the network device table */
  2040. static int remove_net_device(struct hso_device *hso_dev)
  2041. {
  2042. int i;
  2043. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2044. if (network_table[i] == hso_dev) {
  2045. network_table[i] = NULL;
  2046. break;
  2047. }
  2048. }
  2049. if (i == HSO_MAX_NET_DEVICES)
  2050. return -1;
  2051. return 0;
  2052. }
  2053. /* Frees our network device */
  2054. static void hso_free_net_device(struct hso_device *hso_dev)
  2055. {
  2056. int i;
  2057. struct hso_net *hso_net = dev2net(hso_dev);
  2058. if (!hso_net)
  2059. return;
  2060. remove_net_device(hso_net->parent);
  2061. if (hso_net->net)
  2062. unregister_netdev(hso_net->net);
  2063. /* start freeing */
  2064. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2065. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2066. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2067. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2068. }
  2069. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2070. kfree(hso_net->mux_bulk_tx_buf);
  2071. hso_net->mux_bulk_tx_buf = NULL;
  2072. if (hso_net->net)
  2073. free_netdev(hso_net->net);
  2074. kfree(hso_dev);
  2075. }
  2076. static const struct net_device_ops hso_netdev_ops = {
  2077. .ndo_open = hso_net_open,
  2078. .ndo_stop = hso_net_close,
  2079. .ndo_start_xmit = hso_net_start_xmit,
  2080. .ndo_tx_timeout = hso_net_tx_timeout,
  2081. };
  2082. /* initialize the network interface */
  2083. static void hso_net_init(struct net_device *net)
  2084. {
  2085. struct hso_net *hso_net = netdev_priv(net);
  2086. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2087. /* fill in the other fields */
  2088. net->netdev_ops = &hso_netdev_ops;
  2089. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2090. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2091. net->type = ARPHRD_NONE;
  2092. net->mtu = DEFAULT_MTU - 14;
  2093. net->tx_queue_len = 10;
  2094. SET_ETHTOOL_OPS(net, &ops);
  2095. /* and initialize the semaphore */
  2096. spin_lock_init(&hso_net->net_lock);
  2097. }
  2098. /* Adds a network device in the network device table */
  2099. static int add_net_device(struct hso_device *hso_dev)
  2100. {
  2101. int i;
  2102. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2103. if (network_table[i] == NULL) {
  2104. network_table[i] = hso_dev;
  2105. break;
  2106. }
  2107. }
  2108. if (i == HSO_MAX_NET_DEVICES)
  2109. return -1;
  2110. return 0;
  2111. }
  2112. static int hso_rfkill_set_block(void *data, bool blocked)
  2113. {
  2114. struct hso_device *hso_dev = data;
  2115. int enabled = !blocked;
  2116. int rv;
  2117. mutex_lock(&hso_dev->mutex);
  2118. if (hso_dev->usb_gone)
  2119. rv = 0;
  2120. else
  2121. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2122. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2123. USB_CTRL_SET_TIMEOUT);
  2124. mutex_unlock(&hso_dev->mutex);
  2125. return rv;
  2126. }
  2127. static const struct rfkill_ops hso_rfkill_ops = {
  2128. .set_block = hso_rfkill_set_block,
  2129. };
  2130. /* Creates and sets up everything for rfkill */
  2131. static void hso_create_rfkill(struct hso_device *hso_dev,
  2132. struct usb_interface *interface)
  2133. {
  2134. struct hso_net *hso_net = dev2net(hso_dev);
  2135. struct device *dev = &hso_net->net->dev;
  2136. char *rfkn;
  2137. rfkn = kzalloc(20, GFP_KERNEL);
  2138. if (!rfkn)
  2139. dev_err(dev, "%s - Out of memory\n", __func__);
  2140. snprintf(rfkn, 20, "hso-%d",
  2141. interface->altsetting->desc.bInterfaceNumber);
  2142. hso_net->rfkill = rfkill_alloc(rfkn,
  2143. &interface_to_usbdev(interface)->dev,
  2144. RFKILL_TYPE_WWAN,
  2145. &hso_rfkill_ops, hso_dev);
  2146. if (!hso_net->rfkill) {
  2147. dev_err(dev, "%s - Out of memory\n", __func__);
  2148. kfree(rfkn);
  2149. return;
  2150. }
  2151. if (rfkill_register(hso_net->rfkill) < 0) {
  2152. rfkill_destroy(hso_net->rfkill);
  2153. kfree(rfkn);
  2154. hso_net->rfkill = NULL;
  2155. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2156. return;
  2157. }
  2158. }
  2159. static struct device_type hso_type = {
  2160. .name = "wwan",
  2161. };
  2162. /* Creates our network device */
  2163. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2164. int port_spec)
  2165. {
  2166. int result, i;
  2167. struct net_device *net;
  2168. struct hso_net *hso_net;
  2169. struct hso_device *hso_dev;
  2170. hso_dev = hso_create_device(interface, port_spec);
  2171. if (!hso_dev)
  2172. return NULL;
  2173. /* allocate our network device, then we can put in our private data */
  2174. /* call hso_net_init to do the basic initialization */
  2175. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2176. if (!net) {
  2177. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2178. goto exit;
  2179. }
  2180. hso_net = netdev_priv(net);
  2181. hso_dev->port_data.dev_net = hso_net;
  2182. hso_net->net = net;
  2183. hso_net->parent = hso_dev;
  2184. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2185. USB_DIR_IN);
  2186. if (!hso_net->in_endp) {
  2187. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2188. goto exit;
  2189. }
  2190. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2191. USB_DIR_OUT);
  2192. if (!hso_net->out_endp) {
  2193. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2194. goto exit;
  2195. }
  2196. SET_NETDEV_DEV(net, &interface->dev);
  2197. SET_NETDEV_DEVTYPE(net, &hso_type);
  2198. /* registering our net device */
  2199. result = register_netdev(net);
  2200. if (result) {
  2201. dev_err(&interface->dev, "Failed to register device\n");
  2202. goto exit;
  2203. }
  2204. /* start allocating */
  2205. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2206. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2207. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2208. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2209. goto exit;
  2210. }
  2211. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2212. GFP_KERNEL);
  2213. if (!hso_net->mux_bulk_rx_buf_pool[i])
  2214. goto exit;
  2215. }
  2216. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2217. if (!hso_net->mux_bulk_tx_urb) {
  2218. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2219. goto exit;
  2220. }
  2221. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2222. if (!hso_net->mux_bulk_tx_buf)
  2223. goto exit;
  2224. add_net_device(hso_dev);
  2225. hso_log_port(hso_dev);
  2226. hso_create_rfkill(hso_dev, interface);
  2227. return hso_dev;
  2228. exit:
  2229. hso_free_net_device(hso_dev);
  2230. return NULL;
  2231. }
  2232. static void hso_free_tiomget(struct hso_serial *serial)
  2233. {
  2234. struct hso_tiocmget *tiocmget;
  2235. if (!serial)
  2236. return;
  2237. tiocmget = serial->tiocmget;
  2238. if (tiocmget) {
  2239. usb_free_urb(tiocmget->urb);
  2240. tiocmget->urb = NULL;
  2241. serial->tiocmget = NULL;
  2242. kfree(tiocmget);
  2243. }
  2244. }
  2245. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2246. static void hso_free_serial_device(struct hso_device *hso_dev)
  2247. {
  2248. struct hso_serial *serial = dev2ser(hso_dev);
  2249. if (!serial)
  2250. return;
  2251. set_serial_by_index(serial->minor, NULL);
  2252. hso_serial_common_free(serial);
  2253. if (serial->shared_int) {
  2254. mutex_lock(&serial->shared_int->shared_int_lock);
  2255. if (--serial->shared_int->ref_count == 0)
  2256. hso_free_shared_int(serial->shared_int);
  2257. else
  2258. mutex_unlock(&serial->shared_int->shared_int_lock);
  2259. }
  2260. hso_free_tiomget(serial);
  2261. kfree(serial);
  2262. kfree(hso_dev);
  2263. }
  2264. /* Creates a bulk AT channel */
  2265. static struct hso_device *hso_create_bulk_serial_device(
  2266. struct usb_interface *interface, int port)
  2267. {
  2268. struct hso_device *hso_dev;
  2269. struct hso_serial *serial;
  2270. int num_urbs;
  2271. struct hso_tiocmget *tiocmget;
  2272. hso_dev = hso_create_device(interface, port);
  2273. if (!hso_dev)
  2274. return NULL;
  2275. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2276. if (!serial)
  2277. goto exit;
  2278. serial->parent = hso_dev;
  2279. hso_dev->port_data.dev_serial = serial;
  2280. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2281. num_urbs = 2;
  2282. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2283. GFP_KERNEL);
  2284. /* it isn't going to break our heart if serial->tiocmget
  2285. * allocation fails don't bother checking this.
  2286. */
  2287. if (serial->tiocmget) {
  2288. tiocmget = serial->tiocmget;
  2289. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2290. if (tiocmget->urb) {
  2291. mutex_init(&tiocmget->mutex);
  2292. init_waitqueue_head(&tiocmget->waitq);
  2293. tiocmget->endp = hso_get_ep(
  2294. interface,
  2295. USB_ENDPOINT_XFER_INT,
  2296. USB_DIR_IN);
  2297. } else
  2298. hso_free_tiomget(serial);
  2299. }
  2300. }
  2301. else
  2302. num_urbs = 1;
  2303. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2304. BULK_URB_TX_SIZE))
  2305. goto exit;
  2306. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2307. USB_DIR_IN);
  2308. if (!serial->in_endp) {
  2309. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2310. goto exit2;
  2311. }
  2312. if (!
  2313. (serial->out_endp =
  2314. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2315. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2316. goto exit2;
  2317. }
  2318. serial->write_data = hso_std_serial_write_data;
  2319. /* and record this serial */
  2320. set_serial_by_index(serial->minor, serial);
  2321. /* setup the proc dirs and files if needed */
  2322. hso_log_port(hso_dev);
  2323. /* done, return it */
  2324. return hso_dev;
  2325. exit2:
  2326. hso_serial_common_free(serial);
  2327. exit:
  2328. hso_free_tiomget(serial);
  2329. kfree(serial);
  2330. kfree(hso_dev);
  2331. return NULL;
  2332. }
  2333. /* Creates a multiplexed AT channel */
  2334. static
  2335. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2336. int port,
  2337. struct hso_shared_int *mux)
  2338. {
  2339. struct hso_device *hso_dev;
  2340. struct hso_serial *serial;
  2341. int port_spec;
  2342. port_spec = HSO_INTF_MUX;
  2343. port_spec &= ~HSO_PORT_MASK;
  2344. port_spec |= hso_mux_to_port(port);
  2345. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2346. return NULL;
  2347. hso_dev = hso_create_device(interface, port_spec);
  2348. if (!hso_dev)
  2349. return NULL;
  2350. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2351. if (!serial)
  2352. goto exit;
  2353. hso_dev->port_data.dev_serial = serial;
  2354. serial->parent = hso_dev;
  2355. if (hso_serial_common_create
  2356. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2357. goto exit;
  2358. serial->tx_data_length--;
  2359. serial->write_data = hso_mux_serial_write_data;
  2360. serial->shared_int = mux;
  2361. mutex_lock(&serial->shared_int->shared_int_lock);
  2362. serial->shared_int->ref_count++;
  2363. mutex_unlock(&serial->shared_int->shared_int_lock);
  2364. /* and record this serial */
  2365. set_serial_by_index(serial->minor, serial);
  2366. /* setup the proc dirs and files if needed */
  2367. hso_log_port(hso_dev);
  2368. /* done, return it */
  2369. return hso_dev;
  2370. exit:
  2371. if (serial) {
  2372. tty_unregister_device(tty_drv, serial->minor);
  2373. kfree(serial);
  2374. }
  2375. if (hso_dev)
  2376. kfree(hso_dev);
  2377. return NULL;
  2378. }
  2379. static void hso_free_shared_int(struct hso_shared_int *mux)
  2380. {
  2381. usb_free_urb(mux->shared_intr_urb);
  2382. kfree(mux->shared_intr_buf);
  2383. mutex_unlock(&mux->shared_int_lock);
  2384. kfree(mux);
  2385. }
  2386. static
  2387. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2388. {
  2389. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2390. if (!mux)
  2391. return NULL;
  2392. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2393. USB_DIR_IN);
  2394. if (!mux->intr_endp) {
  2395. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2396. goto exit;
  2397. }
  2398. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2399. if (!mux->shared_intr_urb) {
  2400. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2401. goto exit;
  2402. }
  2403. mux->shared_intr_buf =
  2404. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2405. GFP_KERNEL);
  2406. if (!mux->shared_intr_buf)
  2407. goto exit;
  2408. mutex_init(&mux->shared_int_lock);
  2409. return mux;
  2410. exit:
  2411. kfree(mux->shared_intr_buf);
  2412. usb_free_urb(mux->shared_intr_urb);
  2413. kfree(mux);
  2414. return NULL;
  2415. }
  2416. /* Gets the port spec for a certain interface */
  2417. static int hso_get_config_data(struct usb_interface *interface)
  2418. {
  2419. struct usb_device *usbdev = interface_to_usbdev(interface);
  2420. u8 config_data[17];
  2421. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2422. s32 result;
  2423. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2424. 0x86, 0xC0, 0, 0, config_data, 17,
  2425. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2426. return -EIO;
  2427. }
  2428. switch (config_data[if_num]) {
  2429. case 0x0:
  2430. result = 0;
  2431. break;
  2432. case 0x1:
  2433. result = HSO_PORT_DIAG;
  2434. break;
  2435. case 0x2:
  2436. result = HSO_PORT_GPS;
  2437. break;
  2438. case 0x3:
  2439. result = HSO_PORT_GPS_CONTROL;
  2440. break;
  2441. case 0x4:
  2442. result = HSO_PORT_APP;
  2443. break;
  2444. case 0x5:
  2445. result = HSO_PORT_APP2;
  2446. break;
  2447. case 0x6:
  2448. result = HSO_PORT_CONTROL;
  2449. break;
  2450. case 0x7:
  2451. result = HSO_PORT_NETWORK;
  2452. break;
  2453. case 0x8:
  2454. result = HSO_PORT_MODEM;
  2455. break;
  2456. case 0x9:
  2457. result = HSO_PORT_MSD;
  2458. break;
  2459. case 0xa:
  2460. result = HSO_PORT_PCSC;
  2461. break;
  2462. case 0xb:
  2463. result = HSO_PORT_VOICE;
  2464. break;
  2465. default:
  2466. result = 0;
  2467. }
  2468. if (result)
  2469. result |= HSO_INTF_BULK;
  2470. if (config_data[16] & 0x1)
  2471. result |= HSO_INFO_CRC_BUG;
  2472. return result;
  2473. }
  2474. /* called once for each interface upon device insertion */
  2475. static int hso_probe(struct usb_interface *interface,
  2476. const struct usb_device_id *id)
  2477. {
  2478. int mux, i, if_num, port_spec;
  2479. unsigned char port_mask;
  2480. struct hso_device *hso_dev = NULL;
  2481. struct hso_shared_int *shared_int;
  2482. struct hso_device *tmp_dev = NULL;
  2483. if_num = interface->altsetting->desc.bInterfaceNumber;
  2484. /* Get the interface/port specification from either driver_info or from
  2485. * the device itself */
  2486. if (id->driver_info)
  2487. port_spec = ((u32 *)(id->driver_info))[if_num];
  2488. else
  2489. port_spec = hso_get_config_data(interface);
  2490. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2491. dev_err(&interface->dev, "Not our interface\n");
  2492. return -ENODEV;
  2493. }
  2494. /* Check if we need to switch to alt interfaces prior to port
  2495. * configuration */
  2496. if (interface->num_altsetting > 1)
  2497. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2498. interface->needs_remote_wakeup = 1;
  2499. /* Allocate new hso device(s) */
  2500. switch (port_spec & HSO_INTF_MASK) {
  2501. case HSO_INTF_MUX:
  2502. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2503. /* Create the network device */
  2504. if (!disable_net) {
  2505. hso_dev = hso_create_net_device(interface,
  2506. port_spec);
  2507. if (!hso_dev)
  2508. goto exit;
  2509. tmp_dev = hso_dev;
  2510. }
  2511. }
  2512. if (hso_get_mux_ports(interface, &port_mask))
  2513. /* TODO: de-allocate everything */
  2514. goto exit;
  2515. shared_int = hso_create_shared_int(interface);
  2516. if (!shared_int)
  2517. goto exit;
  2518. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2519. if (port_mask & i) {
  2520. hso_dev = hso_create_mux_serial_device(
  2521. interface, i, shared_int);
  2522. if (!hso_dev)
  2523. goto exit;
  2524. }
  2525. }
  2526. if (tmp_dev)
  2527. hso_dev = tmp_dev;
  2528. break;
  2529. case HSO_INTF_BULK:
  2530. /* It's a regular bulk interface */
  2531. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2532. if (!disable_net)
  2533. hso_dev =
  2534. hso_create_net_device(interface, port_spec);
  2535. } else {
  2536. hso_dev =
  2537. hso_create_bulk_serial_device(interface, port_spec);
  2538. }
  2539. if (!hso_dev)
  2540. goto exit;
  2541. break;
  2542. default:
  2543. goto exit;
  2544. }
  2545. /* save our data pointer in this device */
  2546. usb_set_intfdata(interface, hso_dev);
  2547. /* done */
  2548. return 0;
  2549. exit:
  2550. hso_free_interface(interface);
  2551. return -ENODEV;
  2552. }
  2553. /* device removed, cleaning up */
  2554. static void hso_disconnect(struct usb_interface *interface)
  2555. {
  2556. hso_free_interface(interface);
  2557. /* remove reference of our private data */
  2558. usb_set_intfdata(interface, NULL);
  2559. }
  2560. static void async_get_intf(struct work_struct *data)
  2561. {
  2562. struct hso_device *hso_dev =
  2563. container_of(data, struct hso_device, async_get_intf);
  2564. usb_autopm_get_interface(hso_dev->interface);
  2565. }
  2566. static void async_put_intf(struct work_struct *data)
  2567. {
  2568. struct hso_device *hso_dev =
  2569. container_of(data, struct hso_device, async_put_intf);
  2570. usb_autopm_put_interface(hso_dev->interface);
  2571. }
  2572. static int hso_get_activity(struct hso_device *hso_dev)
  2573. {
  2574. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2575. if (!hso_dev->is_active) {
  2576. hso_dev->is_active = 1;
  2577. schedule_work(&hso_dev->async_get_intf);
  2578. }
  2579. }
  2580. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2581. return -EAGAIN;
  2582. usb_mark_last_busy(hso_dev->usb);
  2583. return 0;
  2584. }
  2585. static int hso_put_activity(struct hso_device *hso_dev)
  2586. {
  2587. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2588. if (hso_dev->is_active) {
  2589. hso_dev->is_active = 0;
  2590. schedule_work(&hso_dev->async_put_intf);
  2591. return -EAGAIN;
  2592. }
  2593. }
  2594. hso_dev->is_active = 0;
  2595. return 0;
  2596. }
  2597. /* called by kernel when we need to suspend device */
  2598. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2599. {
  2600. int i, result;
  2601. /* Stop all serial ports */
  2602. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2603. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2604. result = hso_stop_serial_device(serial_table[i]);
  2605. if (result)
  2606. goto out;
  2607. }
  2608. }
  2609. /* Stop all network ports */
  2610. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2611. if (network_table[i] &&
  2612. (network_table[i]->interface == iface)) {
  2613. result = hso_stop_net_device(network_table[i]);
  2614. if (result)
  2615. goto out;
  2616. }
  2617. }
  2618. out:
  2619. return 0;
  2620. }
  2621. /* called by kernel when we need to resume device */
  2622. static int hso_resume(struct usb_interface *iface)
  2623. {
  2624. int i, result = 0;
  2625. struct hso_net *hso_net;
  2626. /* Start all serial ports */
  2627. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2628. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2629. if (dev2ser(serial_table[i])->port.count) {
  2630. result =
  2631. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2632. hso_kick_transmit(dev2ser(serial_table[i]));
  2633. if (result)
  2634. goto out;
  2635. }
  2636. }
  2637. }
  2638. /* Start all network ports */
  2639. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2640. if (network_table[i] &&
  2641. (network_table[i]->interface == iface)) {
  2642. hso_net = dev2net(network_table[i]);
  2643. if (hso_net->flags & IFF_UP) {
  2644. /* First transmit any lingering data,
  2645. then restart the device. */
  2646. if (hso_net->skb_tx_buf) {
  2647. dev_dbg(&iface->dev,
  2648. "Transmitting"
  2649. " lingering data\n");
  2650. hso_net_start_xmit(hso_net->skb_tx_buf,
  2651. hso_net->net);
  2652. hso_net->skb_tx_buf = NULL;
  2653. }
  2654. result = hso_start_net_device(network_table[i]);
  2655. if (result)
  2656. goto out;
  2657. }
  2658. }
  2659. }
  2660. out:
  2661. return result;
  2662. }
  2663. static void reset_device(struct work_struct *data)
  2664. {
  2665. struct hso_device *hso_dev =
  2666. container_of(data, struct hso_device, reset_device);
  2667. struct usb_device *usb = hso_dev->usb;
  2668. int result;
  2669. if (hso_dev->usb_gone) {
  2670. D1("No reset during disconnect\n");
  2671. } else {
  2672. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2673. if (result < 0)
  2674. D1("unable to lock device for reset: %d\n", result);
  2675. else {
  2676. usb_reset_device(usb);
  2677. usb_unlock_device(usb);
  2678. }
  2679. }
  2680. }
  2681. static void hso_serial_ref_free(struct kref *ref)
  2682. {
  2683. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2684. hso_free_serial_device(hso_dev);
  2685. }
  2686. static void hso_free_interface(struct usb_interface *interface)
  2687. {
  2688. struct hso_serial *hso_dev;
  2689. struct tty_struct *tty;
  2690. int i;
  2691. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2692. if (serial_table[i] &&
  2693. (serial_table[i]->interface == interface)) {
  2694. hso_dev = dev2ser(serial_table[i]);
  2695. tty = tty_port_tty_get(&hso_dev->port);
  2696. if (tty) {
  2697. tty_hangup(tty);
  2698. tty_kref_put(tty);
  2699. }
  2700. mutex_lock(&hso_dev->parent->mutex);
  2701. hso_dev->parent->usb_gone = 1;
  2702. mutex_unlock(&hso_dev->parent->mutex);
  2703. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2704. }
  2705. }
  2706. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2707. if (network_table[i] &&
  2708. (network_table[i]->interface == interface)) {
  2709. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2710. /* hso_stop_net_device doesn't stop the net queue since
  2711. * traffic needs to start it again when suspended */
  2712. netif_stop_queue(dev2net(network_table[i])->net);
  2713. hso_stop_net_device(network_table[i]);
  2714. cancel_work_sync(&network_table[i]->async_put_intf);
  2715. cancel_work_sync(&network_table[i]->async_get_intf);
  2716. if (rfk) {
  2717. rfkill_unregister(rfk);
  2718. rfkill_destroy(rfk);
  2719. }
  2720. hso_free_net_device(network_table[i]);
  2721. }
  2722. }
  2723. }
  2724. /* Helper functions */
  2725. /* Get the endpoint ! */
  2726. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2727. int type, int dir)
  2728. {
  2729. int i;
  2730. struct usb_host_interface *iface = intf->cur_altsetting;
  2731. struct usb_endpoint_descriptor *endp;
  2732. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2733. endp = &iface->endpoint[i].desc;
  2734. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2735. (usb_endpoint_type(endp) == type))
  2736. return endp;
  2737. }
  2738. return NULL;
  2739. }
  2740. /* Get the byte that describes which ports are enabled */
  2741. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2742. {
  2743. int i;
  2744. struct usb_host_interface *iface = intf->cur_altsetting;
  2745. if (iface->extralen == 3) {
  2746. *ports = iface->extra[2];
  2747. return 0;
  2748. }
  2749. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2750. if (iface->endpoint[i].extralen == 3) {
  2751. *ports = iface->endpoint[i].extra[2];
  2752. return 0;
  2753. }
  2754. }
  2755. return -1;
  2756. }
  2757. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2758. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2759. struct usb_device *usb, gfp_t gfp)
  2760. {
  2761. int result;
  2762. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2763. usb_rcvintpipe(usb,
  2764. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2765. shared_int->shared_intr_buf,
  2766. 1,
  2767. intr_callback, shared_int,
  2768. shared_int->intr_endp->bInterval);
  2769. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2770. if (result)
  2771. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2772. result);
  2773. return result;
  2774. }
  2775. /* operations setup of the serial interface */
  2776. static const struct tty_operations hso_serial_ops = {
  2777. .open = hso_serial_open,
  2778. .close = hso_serial_close,
  2779. .write = hso_serial_write,
  2780. .write_room = hso_serial_write_room,
  2781. .ioctl = hso_serial_ioctl,
  2782. .set_termios = hso_serial_set_termios,
  2783. .chars_in_buffer = hso_serial_chars_in_buffer,
  2784. .tiocmget = hso_serial_tiocmget,
  2785. .tiocmset = hso_serial_tiocmset,
  2786. .get_icount = hso_get_count,
  2787. .unthrottle = hso_unthrottle
  2788. };
  2789. static struct usb_driver hso_driver = {
  2790. .name = driver_name,
  2791. .probe = hso_probe,
  2792. .disconnect = hso_disconnect,
  2793. .id_table = hso_ids,
  2794. .suspend = hso_suspend,
  2795. .resume = hso_resume,
  2796. .reset_resume = hso_resume,
  2797. .supports_autosuspend = 1,
  2798. .disable_hub_initiated_lpm = 1,
  2799. };
  2800. static int __init hso_init(void)
  2801. {
  2802. int i;
  2803. int result;
  2804. /* put it in the log */
  2805. printk(KERN_INFO "hso: %s\n", version);
  2806. /* Initialise the serial table semaphore and table */
  2807. spin_lock_init(&serial_table_lock);
  2808. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2809. serial_table[i] = NULL;
  2810. /* allocate our driver using the proper amount of supported minors */
  2811. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2812. if (!tty_drv)
  2813. return -ENOMEM;
  2814. /* fill in all needed values */
  2815. tty_drv->driver_name = driver_name;
  2816. tty_drv->name = tty_filename;
  2817. /* if major number is provided as parameter, use that one */
  2818. if (tty_major)
  2819. tty_drv->major = tty_major;
  2820. tty_drv->minor_start = 0;
  2821. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2822. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2823. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2824. tty_drv->init_termios = tty_std_termios;
  2825. hso_init_termios(&tty_drv->init_termios);
  2826. tty_set_operations(tty_drv, &hso_serial_ops);
  2827. /* register the tty driver */
  2828. result = tty_register_driver(tty_drv);
  2829. if (result) {
  2830. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2831. __func__, result);
  2832. goto err_free_tty;
  2833. }
  2834. /* register this module as an usb driver */
  2835. result = usb_register(&hso_driver);
  2836. if (result) {
  2837. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2838. result);
  2839. goto err_unreg_tty;
  2840. }
  2841. /* done */
  2842. return 0;
  2843. err_unreg_tty:
  2844. tty_unregister_driver(tty_drv);
  2845. err_free_tty:
  2846. put_tty_driver(tty_drv);
  2847. return result;
  2848. }
  2849. static void __exit hso_exit(void)
  2850. {
  2851. printk(KERN_INFO "hso: unloaded\n");
  2852. tty_unregister_driver(tty_drv);
  2853. put_tty_driver(tty_drv);
  2854. /* deregister the usb driver */
  2855. usb_deregister(&hso_driver);
  2856. }
  2857. /* Module definitions */
  2858. module_init(hso_init);
  2859. module_exit(hso_exit);
  2860. MODULE_AUTHOR(MOD_AUTHOR);
  2861. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2862. MODULE_LICENSE(MOD_LICENSE);
  2863. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2864. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2865. module_param(debug, int, S_IRUGO | S_IWUSR);
  2866. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2867. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2868. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2869. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2870. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2871. module_param(disable_net, int, S_IRUGO | S_IWUSR);