hso.c 84 KB

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